Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

What does regex' flag 'y' do?

MDN says:

To perform a "sticky" search, that matches starting at the current position in the target string, use the y flag.

I don't quite understand it.

like image 896
DarkLightA Avatar asked Dec 27 '10 22:12

DarkLightA


People also ask

What are flags in regex?

A regular expression consists of a pattern and optional flags: g , i , m , u , s , y . Without flags and special symbols (that we'll study later), the search by a regexp is the same as a substring search. The method str. match(regexp) looks for matches: all of them if there's g flag, otherwise, only the first one.

What is U flag in regex?

Flag u enables the support of Unicode in regular expressions. That means two things: Characters of 4 bytes are handled correctly: as a single character, not two 2-byte characters. Unicode properties can be used in the search: \p{…} .

What is a regex flag Python?

Python regex allows optional flags to specify when using regular expression patterns with match() , search() , and split() , among others. All RE module methods accept an optional flags argument that enables various unique features and syntax variations.

What does $1 do in regex?

For example, the replacement pattern $1 indicates that the matched substring is to be replaced by the first captured group.


1 Answers

Regular expression objects have a lastIndex property, which is used in different ways depending on the g (global) and y (sticky) flags. The y (sticky) flag tells the regular expression to look for a match at lastIndex and only at lastIndex (not earlier or later in the string).

Examples are worth 1024 words:

var str =  "a0bc1"; // Indexes: 01234  var rexWithout = /\d/; var rexWith    = /\d/y;  // Without: rexWithout.lastIndex = 2;          // (This is a no-op, because the regex                                    // doesn't have either g or y.) console.log(rexWithout.exec(str)); // ["0"], found at index 1, because without                                    // the g or y flag, the search is always from                                    // index 0  // With, unsuccessful: rexWith.lastIndex = 2;             // Says to *only* match at index 2. console.log(rexWith.exec(str));    // => null, there's no match at index 2,                                    // only earlier (index 1) or later (index 4)  // With, successful: rexWith.lastIndex = 1;             // Says to *only* match at index 1. console.log(rexWith.exec(str));    // => ["0"], there was a match at index 1.  // With, successful again: rexWith.lastIndex = 4;             // Says to *only* match at index 4. console.log(rexWith.exec(str));    // => ["1"], there was a match at index 4.
.as-console-wrapper {   max-height: 100% !important; }

Compatibility Note:

Firefox's SpiderMonkey JavaScript engine has had the y flag for years, but it wasn't part of the specification until ES2015 (June 2015). Also, for quite a while Firefox had a bug in the handling of the y flag with regard to the ^ assertion, but it was fixed somewhere between Firefox 43 (has the bug) and Firefox 47 (doesn't). Very old versions of Firefox (say, 3.6) did have y and didn't have the bug, so it was a regression that happened later (not defined behavior for the y flag), and then got fixed.

like image 72
T.J. Crowder Avatar answered Sep 20 '22 17:09

T.J. Crowder