RegExp.prototype.sticky

Baseline 已广泛支持

此特性已非常成熟,可在多种设备和浏览器版本上使用。自 ⁨2016 年 9 月⁩以来,它已在各大浏览器中可用。

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

试一试

const str = "table football";
const regex = /foo/y;

regex.lastIndex = 6;

console.log(regex.sticky);
// Expected output: true

console.log(regex.test(str));
// Expected output: true

console.log(regex.test(str));
// Expected output: false

描述

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

sticky 的设置访问器是 undefined。您不能直接更改此属性。

对于粘滞正则表达式和 全局 正则表达式

  • 它们从 lastIndex 开始匹配。
  • 当匹配成功时,lastIndex 会前进到匹配的末尾。
  • lastIndex 超出当前匹配字符串的边界时,lastIndex 会重置为 0。

然而,对于 exec() 方法,匹配失败时的行为有所不同。

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

对于 exec() 方法,同时是粘滞和全局的正则表达式的行为与粘滞但非全局的正则表达式相同。由于 test()exec() 的简单包装器,因此 test() 将忽略全局标志并执行粘滞匹配。但是,由于许多其他方法会特殊处理全局正则表达式的行为,因此全局标志通常与粘滞标志是正交的。

示例

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

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)

锚定粘滞标志

在多个版本中,Firefox 的 SpiderMonkey 引擎在 ^ 断言和粘滞标志方面存在 一个 bug,该 bug 允许以 ^ 断言开头并使用粘滞标志的表达式在不应匹配时进行匹配。此 bug 在 Firefox 3.6(具有粘滞标志但无 bug)之后一段时间引入,并于 2015 年修复。也许是由于这个 bug,规范 专门指出

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

正确行为示例

js
const regex1 = /^foo/y;
regex1.lastIndex = 2;
regex1.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

规范

规范
ECMAScript® 2026 语言规范
# sec-get-regexp.prototype.sticky

浏览器兼容性

另见