mirror of
https://github.com/ruanyf/es6tutorial.git
synced 2025-05-24 18:32:22 +00:00
docs(regex): edit y flag
This commit is contained in:
parent
488983f6d4
commit
1ff6639dfa
@ -346,8 +346,8 @@ let arr2 = Array.from(arrayLike); // ['a', 'b', 'c']
|
||||
```javascript
|
||||
// NodeList对象
|
||||
let ps = document.querySelectorAll('p');
|
||||
Array.from(ps).forEach(function (p) {
|
||||
console.log(p);
|
||||
Array.from(ps).filter(p => {
|
||||
return p.textContent.length > 100;
|
||||
});
|
||||
|
||||
// arguments对象
|
||||
|
@ -216,28 +216,6 @@ REGEX.lastIndex // 4
|
||||
|
||||
上面代码由于不能保证头部匹配,所以返回`null`。`y`修饰符的设计本意,就是让头部匹配的标志`^`在全局匹配中都有效。
|
||||
|
||||
在`split`方法中使用`y`修饰符,原字符串必须以分隔符开头。这也意味着,只要匹配成功,数组的第一个成员肯定是空字符串。
|
||||
|
||||
```javascript
|
||||
// 没有找到匹配
|
||||
'x##'.split(/#/y)
|
||||
// [ 'x##' ]
|
||||
|
||||
// 找到两个匹配
|
||||
'##x'.split(/#/y)
|
||||
// [ '', '', 'x' ]
|
||||
```
|
||||
|
||||
后续的分隔符只有紧跟前面的分隔符,才会被识别。
|
||||
|
||||
```javascript
|
||||
'#x#'.split(/#/y)
|
||||
// [ '', 'x#' ]
|
||||
|
||||
'##'.split(/#/y)
|
||||
// [ '', '', '' ]
|
||||
```
|
||||
|
||||
下面是字符串对象的`replace`方法的例子。
|
||||
|
||||
```javascript
|
||||
|
Loading…
x
Reference in New Issue
Block a user