-
Notifications
You must be signed in to change notification settings - Fork 77
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
yibuyisheng
committed
Mar 24, 2016
1 parent
1ab2a2d
commit f962d74
Showing
12 changed files
with
247 additions
and
49 deletions.
There are no files selected for viewing
File renamed without changes.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,10 @@ | ||
## 6.2 Unicode 码点解析 | ||
|
||
在 ECMAScript 6 中,存在一种新的 Unicode 码点解析方式:能够指定任何编码方式(即便这种编码方式超过了16位): | ||
|
||
```js | ||
console.log('\u{1F680}'); // ES6: 一个码点 | ||
console.log('\uD83D\uDE80'); // ES5: 两个码点单元 | ||
``` | ||
|
||
本章关于 Unicode 的部分展示了更多信息。 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,29 @@ | ||
## 6.3 字符串插值,多行字符串字面量和原始字符串字面量 | ||
|
||
模板字面量有专门的章节深入讲述,其提供了三种有意思的特性。 | ||
|
||
首先,模板字面量支持字符串插值: | ||
|
||
```js | ||
const first = 'Jane'; | ||
const last = 'Doe'; | ||
console.log(`Hello ${first} ${last}!`); | ||
// Hello Jane Doe! | ||
``` | ||
|
||
其次,模板字面量可以跨多行: | ||
|
||
```js | ||
const multiLine = ` | ||
This is | ||
a string | ||
with multiple | ||
lines`; | ||
``` | ||
|
||
最后,如果在模板字面量前面加上*标签* `String.raw` ,那么就变成了“原生的”的字符串了,反斜杠再也不是用于转义的特殊字符,也就是说, `\n` 不会转义成换行符: | ||
|
||
```js | ||
const str = String.raw`Not a newline: \n`; | ||
console.log(str === 'Not a newline: \\n'); // true | ||
``` |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,66 @@ | ||
## 6.4 字符串遍历 | ||
|
||
字符串是可遍历的,也就是说可以使用 `for-of` 遍历字符串中的字符: | ||
|
||
```js | ||
for (const ch of 'abc') { | ||
console.log(ch); | ||
} | ||
// Output: | ||
// a | ||
// b | ||
// c | ||
``` | ||
|
||
也可以使用扩展操作符(...)将字符串转换成数组: | ||
|
||
```js | ||
const chars = [...'abc']; | ||
// ['a', 'b', 'c'] | ||
``` | ||
|
||
### 6.4.1 字符串遍历遵循 Unicode 码点规则 | ||
|
||
字符串遍历的时候,分割字符串是按照码点边界来的,这意味着遍历的时候拿到的字符可能由一个或者两个 JavaScript 字符组成: | ||
|
||
```js | ||
for (const ch of 'x\uD83D\uDE80y') { | ||
console.log(ch.length); | ||
} | ||
// Output: | ||
// 1 | ||
// 2 | ||
// 1 | ||
``` | ||
|
||
### 6.4.2 计算码点数 | ||
|
||
遍历是一种快速计算字符串 Unicode 码点数的方式: | ||
|
||
``` | ||
> [...'x\uD83D\uDE80y'].length | ||
3 | ||
``` | ||
|
||
### 6.4.3 翻转包含非 BMP 码点的字符串 | ||
|
||
利用遍历也能够实现翻转包含非 BMP 码点(比16位大,用两个 JavaScript 字符编码)的字符串: | ||
|
||
```js | ||
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' | ||
``` | ||
|
||
在火狐浏览器中的翻转结果: | ||
|
||
 | ||
|
||
> 遗留问题:合并标记 | ||
> 一个*合并标记*就是两个 Unicode 码点的序列,用于展示单个符号。我在这里展示的 ES6 翻转非 BMP 码点的方法,不适用于合并标记。对于合并标记的翻转,你需要一个库,例如 Mathias Bynens 的 [Esrever](https://github.com/mathiasbynens/esrever) 。 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,29 @@ | ||
## 6.5 码点的数字值 | ||
|
||
新方法 `codePointAt()` 返回字符串中指定位置字符的数字值: | ||
|
||
```js | ||
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 | ||
``` | ||
|
||
该方法在字符串遍历中也能正确执行: | ||
|
||
```js | ||
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 | ||
``` |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,26 @@ | ||
## 6.6 检查子串 | ||
|
||
有三个方法用于检查某个字符串是否是另一个字符串的子串: | ||
|
||
``` | ||
> '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 | ||
``` |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,8 @@ | ||
## 6.7 重复字符串 | ||
|
||
`repeat()` 方法用于生成重复字符串: | ||
|
||
``` | ||
> 'doo '.repeat(3) | ||
'doo doo doo ' | ||
``` |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,10 @@ | ||
## 6.8 用正则表达式作为参数的字符串方法 | ||
|
||
在 ES6 中,四个用正则表达式作为参数的字符串方法做的事情相当少,它们主要调用参数上面的方法: | ||
|
||
* `String.prototype.match(regexp)` 调用 `regexp[Symbol.match](this)`。 | ||
* `String.prototype.replace(searchValue, replaceValue)` 调用 `searchValue[Symbol.replace](this, replaceValue)` 。 | ||
* `String.prototype.search(regexp)` 调用 `regexp[Symbol.search](this)` 。 | ||
* `String.prototype.split(separator, limit)` 调用 `separator[Symbol.split](this, limit)` 。 | ||
|
||
这些参数根本不需要是正则表达式,任何带有相应方法的对象都可以作为参数。 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,49 @@ | ||
## 6.9 备忘:新的字符串方法 | ||
|
||
标签模板: | ||
|
||
* String.raw(callSite, ...substitutions) : string | ||
|
||
用于获取“原始”字符串内容的模板标签(反斜杠不再是转义字符): | ||
|
||
``` | ||
> String.raw`\` === '\\' | ||
true | ||
``` | ||
更多内容可阅读关于模板字面量的章节。 | ||
Unicode 和码点: | ||
* String.fromCodePoint(...codePoints : number[]) : string | ||
将数字值转换成 Unicode 码点字,然后返回由码点构成的字符串。 | ||
* String.prototype.codePointAt(pos) : number | ||
返回在从位置 `pos` 处开始的码点的数字值(由一个或者两个 JavaScript 字符组成)。 | ||
* String.prototype.normalize(form? : string) : string | ||
不同的码点组合可能看起来是一样的。 [Unicode 标准化](http://unicode.org/faq/normalization.html) 将它们修正为相同的*标准值*。这对相等比较和字符串搜索很有帮助。对于一般的文本,建议使用 `NFC` 形式。 | ||
查找字符串: | ||
* String.prototype.startsWith(searchString, position=0) : boolean | ||
`position` 参数指定了字符串的开始搜索位置。 | ||
* String.prototype.endsWith(searchString, endPosition=searchString.length) : boolean | ||
`endPosition` 指定了字符串的结束搜索位置。 | ||
* String.prototype.includes(searchString, position=0) : boolean | ||
从字符串 `position` 位置开始搜索,是否包含 `searchString` 子串。 | ||
重复字符串: | ||
* String.prototype.repeat(count) : string | ||
返回重复指定次数的字符串。 |
File renamed without changes.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters