RegExp.prototype.sticky

RegExp 实例的 sticky 访问器属性返回此正则表达式是否使用 y 标志。

¥The sticky accessor property of RegExp instances returns whether or not the y flag is used with this regular expression.

Try it

描述

¥Description

如果使用了 y 标志,则 RegExp.prototype.sticky 的值为 true;否则,falsey 标志指示正则表达式仅尝试从 lastIndex 属性指示的索引匹配目标字符串(与全局正则表达式不同,它不会尝试从任何后续索引匹配)。

¥RegExp.prototype.sticky has the value true if the y flag was used; otherwise, false. The y flag indicates that the regex attempts to match the target string only from the index indicated by the lastIndex property (and unlike a global regex, does not attempt to match from any later indexes).

sticky 的集合访问器是 undefined。你不能直接更改此属性。

¥The set accessor of sticky is undefined. You cannot change this property directly.

对于粘性正则表达式和 global 正则表达式:

¥For both sticky regexes and global regexes:

  • 他们从 lastIndex 开始匹配。
  • 当比赛成功时,lastIndex 晋级到比赛结束。
  • lastIndex 超出当前匹配字符串的范围时,lastIndex 重置为 0。

但是,对于 exec() 方法,匹配失败时的行为有所不同:

¥However, for the exec() method, the behavior when matching fails is different:

  • 当对粘性正则表达式调用 exec() 方法时,如果正则表达式在 lastIndex 处无法匹配,则正则表达式立即返回 null 并将 lastIndex 重置为 0。
  • 当对全局正则表达式调用 exec() 方法时,如果正则表达式在 lastIndex 处无法匹配,它将尝试从下一个字符开始匹配,依此类推,直到找到匹配项或到达字符串末尾。

对于 exec() 方法,粘性和全局正则表达式的行为与粘性和非全局正则表达式相同。因为 test()exec() 的简单封装,所以 test() 将忽略全局标志并执行粘性匹配。然而,由于许多其他方法对全局正则表达式的行为进行了特殊处理,因此全局标志通常与粘性标志正交。

¥For the exec() method, a regex that's both sticky and global behaves the same as a sticky and non-global regex. Because test() is a simple wrapper around exec(), test() would ignore the global flag and perform sticky matches as well. However, due to many other methods special-casing the behavior of global regexes, the global flag is, in general, orthogonal to the sticky flag.

示例

¥Examples

使用带有粘性标志的正则表达式

¥Using a regular expression with the sticky flag

js
const str = "#foo#";
const 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)

锚定粘性标志

¥Anchored sticky flag

对于多个版本,Firefox 的 SpiderMonkey 引擎对于 ^ 断言和粘性标志具有 一个错误,粘性标志允许以 ^ 断言开头的表达式,并在不应该匹配时使用粘性标志进行匹配。该错误是在 Firefox 3.6(有粘性标志但没有该错误)之后的一段时间引入的,并于 2015 年修复。也许是因为 bug,规范 特别呼吁 的事实是:

¥For several versions, Firefox's SpiderMonkey engine had a bug with regard to the ^ assertion and the sticky flag which allowed expressions starting with the ^ assertion and using the sticky flag to match when they shouldn't. The bug was introduced some time after Firefox 3.6 (which had the sticky flag but not the bug) and fixed in 2015. Perhaps because of the bug, the specification specifically calls out the fact that:

即使当 y 标志与模式一起使用时,^ 也始终仅在输入的开头匹配,或者(如果 rer.[[Multiline]] 为 true)在行的开头匹配。

¥Even when the y flag is used with a pattern, ^ always matches only at the beginning of Input, or (if rer.[[Multiline]] is true) at the beginning of a line.

正确行为的示例:

¥Examples of correct behavior:

js
const regex = /^foo/y;
regex.lastIndex = 2;
regex.test("..foo"); // false - index 2 is not the beginning of the string

const 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
ECMAScript Language Specification
# sec-get-regexp.prototype.sticky

¥Specifications

浏览器兼容性

BCD tables only load in the browser

¥Browser compatibility

也可以看看