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 pattern = /\w+/y;
// Start match from position 4
pattern.lastIndex = 4;
let result = text.match(pattern);
let text = "abc def ghi";
let pattern = /\w+/;
// Start match from position 4
pattern.lastIndex = 4;
let result = text.match(pattern);
Try it Yourself »let pattern = /\w+/;
// Start match from position 4
pattern.lastIndex = 4;
let result = text.match(pattern);
Description
The/y (Sticky) flag performs a "sticky" search that matches only from the lastIndexproperty of the RegExp object.
The/y flag ensures that subsequent matches start immediatelyafter the previous one, without skipping characters.
Syntax
new RegExp("regexp", "y")
or simply:
/regexp/y
or simply:
/regexp/y
See Also:
The Corresponding Property:sticky
Regular Expression Methods
Regular ExpressionSearch andReplace can be done with different methods.
These are the most common:
String Methods
| Method | Description |
|---|---|
| match(regex) | Returns an Array of results |
| matchAll(regex) | Returns an Iterator of results |
| replace(regex) | Returns a new String |
| replaceAll(regex) | Returns a new String |
| search(regex) | Returns the index of the first match |
| split(regex) | Returns an Array of results |
RegExp Methods
| Method | Description |
|---|---|
| regex.exec() | Returns an Iterator of results |
| regex.test() | Returns true or false |
Browser Support
/regexp/y is an ECMAScript6 (ES6 2015) feature.
JavaScript 2015 is supported in all browsers sinceJune 2017:
| Chrome 51 | Edge 15 | Firefox 54 | Safari 10 | Opera 38 |
| May 2016 | Apr 2017 | Jun 2017 | Sep 2016 | Jun 2016 |

