RegExp /y Flag
Examples
let text = "abc def ghi";
let pattern = /\w+/y;
// Start match from position 4
pattern.lastIndex = 4;
let result = text.match(pattern);
Try it Yourself »
let text = "abc def ghi";
let pattern = /\w+/;
// Start match from position 4
pattern.lastIndex = 4;
let result = text.match(pattern);
Try it Yourself »
Description
The /y (Sticky) flag performs a "sticky" search that matches only from the lastIndex property of the RegExp object.
The /y flag ensures that subsequent matches start immediately after the previous one, without skipping characters.
Syntax
new RegExp("regexp", "y")
or simply:
/regexp/y
See Also:
The Corresponding Property: sticky
Regular Expression Search Methods
In JavaScript, a regular expression text search, can be done with different methods.
With a pattern as a regular expression, these are the most common methods:
String Methods
match(pattern) | An Array of results |
matchAll(pattern) | An Iterator of results |
replace(pattern, rep) | A new String |
search(pattern) | Index of the first match |
split(pattern) | An Array of results |
RegExp Methods
pattern.exec() | An Iterator of results |
pattern.test() | true or false |
Browser Support
/regexp/y
is an ECMAScript6 (ES6 2015) feature.
JavaScript 2015 is supported in all browsers since June 2017:
Chrome 51 | Edge 15 | Firefox 54 | Safari 10 | Opera 38 |
May 2016 | Apr 2017 | Jun 2017 | Sep 2016 | Jun 2016 |