RegExp sticky flag (y)

Anchors each match attempt exactly at lastIndex.

Since ES2015 (ES6) Spec ↗

Syntax

/pattern/y

Examples

const re = /\d/y;
re.lastIndex = 1;
console.log(re.exec("a1b"));
Output
null
const re = /\d/y;
re.lastIndex = 1;
console.log(re.exec("12")[0]);
console.log(re.exec("12")[0]);
Output
2
null
console.log(/a/y.test("ba"));
Output
false

Notes

- Unlike `g`, the match must begin exactly at `lastIndex`; it does not scan forward. - Useful for tokenizers that consume input position by position. - Check with `regexp.sticky`.

Browser & runtime support

EnvironmentSince version
chrome 49
firefox 3
safari 10
edge 13
node 6.0

See also