T.J*_*der 46
正则表达式对象具有lastIndex属性,该属性根据g(全局)和y(粘性)标志以不同方式使用.该y(粘)标志告诉正则表达式查找匹配的lastIndex,并只在lastIndex(不得提前或推迟的字符串).
示例值1024字:
var str = "a0bc1";
// Indexes: 01234
var rexWithout = /\d/;
var rexWith = /\d/y;
// Without:
rexWithout.lastIndex = 2; // (This is a no-op, because the regex
// doesn't have either g or y.)
console.log(rexWithout.exec(str)); // ["0"], found at index 1, because without
// the g or y flag, the search is always from
// index 0
// With, unsuccessful:
rexWith.lastIndex = 2; // Says to *only* match at index 2.
console.log(rexWith.exec(str)); // => null, there's no match at index 2,
// only earlier (index 1) or later (index 4)
// With, successful:
rexWith.lastIndex = 1; // Says to *only* match at index 1.
console.log(rexWith.exec(str)); // => ["0"], there was a match at index 1.
// With, successful again:
rexWith.lastIndex = 4; // Says to *only* match at index 4.
console.log(rexWith.exec(str)); // => ["1"], there was a match at index 4.Run Code Online (Sandbox Code Playgroud)
.as-console-wrapper {
max-height: 100% !important;
}Run Code Online (Sandbox Code Playgroud)
兼容性说明:
Firefox的SpiderMonkey JavaScript引擎已经有y多年的标志,但在ES2015(2015年6月)之前它不是规范的一部分.此外,很长一段时间Firefox 在处理y有关^断言的标志时遇到了错误,但它在Firefox 43(有bug)和Firefox 47(没有)之间的某处修复.很老版本的Firefox(比方说,3.6)确实有y并且没有错误,所以它是后来发生的回归(没有为y标志定义行为),然后得到修复.
| 归档时间: |
|
| 查看次数: |
6737 次 |
| 最近记录: |