![]() |
VOOZH | about |
In JavaScript, the y modifier is used in regular expressions for sticky matching, meaning the regular expression search will only start at the position where the last match ended.
Unlike other modifiers, the y modifier ensures that each match is found only if it starts at the exact position in the string, preventing the regex from skipping over characters. This makes it ideal when you need precise, non-overlapping matches in sequential searches.
null null
The y modifier is ideal for matching multiple parts of a string in sequence, without allowing overlaps. Here’s an example:
hello at position 0 hello at position 6 hello at position 12
The y modifier works well with anchors like ^ and $ (start and end of the string), ensuring that the match is found precisely at the required position.
[ 'hello', index: 0, input: 'hello123abc456', groups: undefined ] lastIndex after 1st match: 5 null lastIndex after 2nd match: 0