es6 日期字符串转日期_ES6中字符串新特性

新的API方法:字符串检测

三种新方法检查另一个字符串中是否存在字符串:

> 'hello'.startsWith('hell') // 返回布尔值,表示参数字符串是否在原字符串的头部

true

> 'hello'.endsWith('ello') // 返回布尔值,表示参数字符串是否在原字符串的尾部。

true

> 'hello'.includes('ell') // 返回布尔值,表示是否找到了参数字符串

true

这些方法都有一个可选的第二个参数,该参数指定要搜索的字符串开始或结束的位置:

> 'hello'.startsWith('ello', 1)

true

> 'hello'.endsWith('hell', 4)

true

> 'hello'.includes('ell', 1)

true

> 'hello'.includes('ell', 2)

false

字符串复制

repeat()方法重复字符串.

> 'doo '.repeat(3) // 返回一个新字符串,表示将原字符串重复n次

'doo doo doo '

除了上述新增加的的API,ES6关于字符串还增加了一种新的表示方法---模板字符串

格式:``

// String interpolation via template literals (in backticks)

// 通过${}插入变量

const first = 'Jane';

const last = 'Doe';

console.log(`Hello ${first} ${last}!`);

// Hello Jane Doe!

// Template literals also let you create strings with multiple lines

// 支持多行字符串

const multiLine = `

This is

a string

with multiple

lines`;

Unicode码点转义

在ECMAScript 6中,有一种新的Unicode转义,可让您指定任何代码点(甚至是那些超过16位的代码点):

console.log('\u{1F680}'); // ES6: single code point

console.log('\uD83D\uDE80'); // ES5: two code units

模板字符串和String.raw()

关于模板字符串我会开一篇文章专门介绍.这里主要介绍模板字符串三个特性、支持字符串插值

const first = 'Jane';

const last = 'Doe';

console.log(`Hello ${first} ${last}!`);

// Hello Jane Doe!支持多行字符串表示方式

const multiLine = `

This is

a string

with multiple

lines`;ES6 为原生的 String 对象,提供了一个raw方法。String.raw方法,往往用来充当模板字符串的处理函数,返回一个斜杠都被转义(即斜杠前面再加一个斜杠)的字符串,对应于替换变量后的模板字符串。

const str = String.raw`Not a newline: \n`;

console.log(str === 'Not a newline: \\n'); // true

迭代字符串

字符串是可迭代的,这意味着您可以使用for-of遍历它们的字符:

for (const ch of 'abc') {

console.log(ch);

}

// Output:

// a

// b

// c

同样你可以使用扩展运算符(...)将字符串转换为数组:

const chars = [...'abc'];

// ['a', 'b', 'c']

Unicode代码点同样是可以迭代的。

字符串迭代器沿着代码点边界分割字符串,这意味着它返回的字符串包含一个或两个JavaScript字符:

for (const ch of 'x\uD83D\uDE80y') {

console.log(ch.length);

}

// Output:

// 1

// 2

// 1

计算代码点长度

迭代器为您提供了一种快速计算字符串中Unicode代码点的方法:

> [...'x\uD83D\uDE80y'].length

3

颠倒非BMP代码点的字符串

迭代器还可以反转包含非BMP代码点(大于16位并编码为两个JavaScript字符)的字符串:

const str = 'x\uD83D\uDE80y';

// ES5: \uD83D\uDE80 are (incorrectly) reversed

console.log(str.split('').reverse().join(''));

// 'y\uDE80\uD83Dx'

// ES6: order of \uD83D\uDE80 is preserved

console.log([...str].reverse().join(''));

// 'y\uD83D\uDE80x'

codePointAt

ES6 提供了codePointAt方法,能够正确处理 4 个字节储存的字符,返回一个字符的码点。

const str = 'x\uD83D\uDE80y';

console.log(str.codePointAt(0).toString(16)); // 78

console.log(str.codePointAt(1).toString(16)); // 1f680

console.log(str.codePointAt(3).toString(16)); // 79

该方法配合字符串迭代遍历使用非常方便:

for (const ch of 'x\uD83D\uDE80y') {

console.log(ch.codePointAt(0).toString(16));

}

// Output:

// 78

// 1f680

// 79

codePointAt()的相反方法是String.fromCodePoint():

> String.fromCodePoint(0x78, 0x1f680, 0x79) === 'x\uD83D\uDE80y'

true

那些接受正则表达式作为其参数的字符串方法

String.prototype.match(regexp) calls

regexp[Symbol.match](this).

String.prototype.replace(searchValue, replaceValue) calls

searchValue[Symbol.replace](this, replaceValue).

String.prototype.search(regexp) calls

regexp[Symbol.search](this).

String.prototype.split(separator, limit) calls

separator[Symbol.split](this, limit).

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值