regExp.sticky
sticky
属性反映了搜索是否具有粘性( 仅从正则表达式的lastIndex
属性表示的索引处搜索 )。sticky
是正则表达式对象的只读属性。
| RegExp.prototype.sticky
属性的属性特性 |
|:----|
| Writable | no |
| Enumerable | no |
| Configurable | yes |
描述
你不能直接更改这个属性,它是只读的。
示例
使用带 sticky 标志的正则表达式
var str = '#foo#';
var regex = /foo/y;
regex.lastIndex = 1;
regex.test(str); // true
regex.lastIndex = 5;
regex.test(str); // false (lastIndex is taken into account with sticky flag)
regex.lastIndex; // 0 (reset after match failure)
锚定的 sticky 标志
当使用带有y标识的匹配模式时,^断言总是会匹配输入的开始位置或者(如果是多行模式)每一行的开始位置。
正确行为的示例:
var regex = /^foo/y;
regex.lastIndex = 2;
regex.test('..foo'); // false - index 2 is not the beginning of the string
var regex2 = /^foo/my;
regex2.lastIndex = 2;
regex2.test('..foo'); // false - index 2 is not the beginning of the string or line
regex2.lastIndex = 2;
regex2.test('.\nfoo'); // true - index 2 is the beginning of a line
规范
Specification | Status | Comment |
---|---|---|
ECMAScript 2015 (6th Edition, ECMA-262)The definition of 'RegExp.prototype.sticky' in that specification. | Standard | Initial definition. |
ECMAScript Latest Draft (ECMA-262)The definition of 'RegExp.prototype.sticky' in that specification. | Draft | |
浏览器兼容性
Feature | Chrome | Edge | Firefox (Gecko) | Internet Explorer | Opera | Safari |
---|---|---|---|---|---|---|
Basic support | 49 | 13 | 3.0 (1.9) | No support | 36 | 10 |
Prototype accessor property | ? | (Yes) | 38 (38) | No support | ? | ? |
Anchored sticky(y) flag behavior per ES2015 | ? | (Yes) | 44 (44) | No support | ? | ? |
Feature | Android | Chrome for Android | Edge | Firefox Mobile (Gecko) | IE Mobile | Opera Mobile | Safari Mobile |
---|---|---|---|---|---|---|---|
Basic support | No support | 49 | (Yes) | 1.0 (1.9) | No support | (Yes) | 10 |
Prototype accessor property | No support | ? | (Yes) | 38.0 (38) | No support | ? | ? |
Anchored sticky(y) flag behavior per ES2015 | No support | ? | (Yes) | 44.0 (44) | No support | ? | ? |
本文档系腾讯云开发者社区成员共同维护,如有问题请联系 cloudcommunity@tencent.com