regex' flag 'y'是做什么的?

[英]What does regex' flag 'y' do?


MDN says:

MDN說:

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

要執行“粘性”搜索(從目標字符串的當前位置開始匹配),請使用y標志。

I don't quite understand it.

我不太明白。

3 个解决方案

#1


33  

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).

正則表達式對象有一個lastIndex屬性,根據g(全局)和y(粘性)標志以不同的方式使用它。y(粘性)標志告訴正則表達式在lastIndex和lastIndex(不在字符串的前面或后面)查找匹配。

Examples are worth 1024 words:

例子價值1024字:

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.

Firefox的SpiderMonkey JavaScript引擎已經使用了多年的y標志,但直到2015年(2015年6月)才成為規范的一部分。Firefox也一段時間有一個錯誤處理的y國旗^斷言,但它是固定在Firefox 43(有缺陷)和Firefox 47(不)。非常舊的Firefox版本(比如3.6)確實有y,並且沒有bug,所以這是后來發生的回歸(沒有為y標志定義行為),然后被修復。

#2


3  

You can find an example here:

你可以在這里找到一個例子:

https://developer.mozilla.org/en/JavaScript/Reference/Global_Objects/RegExp#Example.3a_Using_a_regular_expression_with_the_.22sticky.22_flag

https://developer.mozilla.org/en/JavaScript/Reference/Global_Objects/RegExp Example.3a_Using_a_regular_expression_with_the_.22sticky.22_flag

#3


0  

You can find an example here:

你可以在這里找到一個例子:

https://developer.mozilla.org/en/JavaScript/Reference/Global_Objects/RegExp#Example.3a_Using_a_regular_expression_with_the_.22sticky.22_flag

https://developer.mozilla.org/en/JavaScript/Reference/Global_Objects/RegExp Example.3a_Using_a_regular_expression_with_the_.22sticky.22_flag

Except that IMHO the example should be

除了那個例子

var regex = /^(\S+) line\n?/y;

instead of

而不是

var regex = /(\S+) line\n?/y;

since with this line the code gives the same result whether you say

因為無論你說什么,這行代碼都會給出相同的結果

var regex = /(\S+) line\n?/y;

or

var regex = /(\S+) line\n?/g;

注意!

本站翻译的文章,版权归属于本站,未经许可禁止转摘,转摘请注明本文地址:https://www.itdaan.com/blog/2010/12/27/97baf02d6f2dc68bfe405ae8bc77a36a.html



 
粤ICP备14056181号  © 2014-2021 ITdaan.com