RegExp sticky flag (y)
Anchors each match attempt exactly at lastIndex.
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
| Environment | Since version |
|---|---|
| chrome | 49 |
| firefox | 3 |
| safari | 10 |
| edge | 13 |
| node | 6.0 |