File tree Expand file tree Collapse file tree 1 file changed +4
-4
lines changed Expand file tree Collapse file tree 1 file changed +4
-4
lines changed Original file line number Diff line number Diff line change @@ -38,7 +38,7 @@ new RegExp(/abc/ig, 'i').flags
38
38
39
39
## 字符串的正则方法
40
40
41
- 字符串对象共有 4 个方法,可以使用正则表达式:` match() ` 、` replace() ` 、` search() ` 和` split() ` 。
41
+ ES6 出现之前, 字符串对象共有 4 个方法,可以使用正则表达式:` match() ` 、` replace() ` 、` search() ` 和` split() ` 。
42
42
43
43
ES6 将这 4 个方法,在语言内部全部调用` RegExp ` 的实例方法,从而做到所有与正则相关的方法,全都定义在` RegExp ` 对象上。
44
44
@@ -507,9 +507,9 @@ ES2018 引入了[具名组匹配](https://github.com/tc39/proposal-regexp-named-
507
507
const RE_DATE = / (?<year>\d {4} )-(?<month>\d {2} )-(?<day>\d {2} )/ ;
508
508
509
509
const matchObj = RE_DATE .exec (' 1999-12-31' );
510
- const year = matchObj .groups .year ; // 1999
511
- const month = matchObj .groups .month ; // 12
512
- const day = matchObj .groups .day ; // 31
510
+ const year = matchObj .groups .year ; // " 1999"
511
+ const month = matchObj .groups .month ; // "12"
512
+ const day = matchObj .groups .day ; // "31"
513
513
```
514
514
515
515
上面代码中,“具名组匹配”在圆括号内部,模式的头部添加“问号 + 尖括号 + 组名”(` ?<year> ` ),然后就可以在` exec ` 方法返回结果的` groups ` 属性上引用该组名。同时,数字序号(` matchObj[1] ` )依然有效。
You can’t perform that action at this time.
0 commit comments