RegExp y Modifier
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
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:
Example | Description |
---|---|
text.match(pattern) | The String method match() |
text.search(pattern) | The String method search() |
pattern.exec(text) | The RexExp method exec() |
pattern.test(text) | The RegExp method test() |
Browser Support
/regexp/y
is an ECMAScript6 (ES6 2015) feature.
JavaScript 2015 is fully supported in all modern browsers since June 2017:
Chrome 51 | Edge 15 | Firefox 54 | Safari 10 | Opera 38 |
May 2016 | Apr 2017 | Jun 2017 | Sep 2016 | Jun 2016 |