JavaScript RegExps中"y"粘性模式修饰符的作用是什么? [英] What is the purpose of the 'y' sticky pattern modifier in JavaScript RegExps?
问题描述
MDN为JavaScript RegExp引入了"y"粘滞标记.这是文档摘录:>
MDN introduced the 'y' sticky flag for JavaScript RegExp. Here is a documentation excerpt:
y
粘性;仅与目标字符串中此正则表达式的lastIndex属性指示的索引匹配(并且不尝试与任何后续索引匹配).
sticky; matches only from the index indicated by the lastIndex property of this regular expression in the target string (and does not attempt to match from any later indexes).
还有一个例子:
var text = 'First line\nSecond line';
var regex = /(\S+) line\n?/y;
var match = regex.exec(text);
console.log(match[1]); // prints 'First'
console.log(regex.lastIndex); // prints '11'
var match2 = regex.exec(text);
console.log(match2[1]); // prints 'Second'
console.log(regex.lastIndex); // prints '22'
var match3 = regex.exec(text);
console.log(match3 === null); // prints 'true'
但是在这种情况下, g 全局标志的使用实际上没有任何区别:
But there isn't actually any difference between the usage of the g global flag in this case:
var text = 'First line\nSecond line';
var regex = /(\S+) line\n?/g;
var match = regex.exec(text);
console.log(match[1]); // prints 'First'
console.log(regex.lastIndex); // prints '11'
var match2 = regex.exec(text);
console.log(match2[1]); // prints 'Second'
console.log(regex.lastIndex); // prints '22'
var match3 = regex.exec(text);
console.log(match3 === null); // prints 'true'
相同的输出.因此,我猜想可能还有其他关于'y'标志的问题,并且MDN的示例似乎不是此修饰符的真实用例,因为它似乎可以代替'g'全局修饰符.
Same output. So I guess there might be something else regarding the 'y' flag and it seems that MDN's example isn't a real use-case for this modifier, as it seems to just work as a replacement for the 'g' global modifier here.
那么,这个实验性"y"粘滞标记的实际用例是什么?仅从RegExp.lastIndex属性进行匹配"的目的是什么?和 RegExp.prototype.exec
一起使用时,它与'g'有何不同?
So, what could be a real use-case for this experimental 'y' sticky flag? What's its purpose in "matching only from the RegExp.lastIndex property" and what makes it differ from 'g' when used with RegExp.prototype.exec
?
感谢您的关注.
推荐答案
The difference between y
and g
is described in Practical Modern JavaScript:
粘滞标记像
g
一样前进lastIndex
,但是仅当找到匹配项时从lastIndex
开始,没有前向搜索.添加了粘滞标记以提高使用以下命令编写词法分析器的性能JavaScript ...
The sticky flag advances
lastIndex
likeg
but only if a match is found starting atlastIndex
, there is no forward search. The sticky flag was added to improve the performance of writing lexical analyzers using JavaScript...
对于实际用例
可以用于要求从位置
n
开始的正则表达式匹配,其中n
是lastIndex
设置的.如果是非多行常规表达式,带有粘滞标记的lastIndex
值为0
效果与以^
开始正则表达式相同要求匹配从搜索到的文本的开头开始.
It could be used to require a regular expression match starting at position
n
wheren
is whatlastIndex
is set to. In the case of a non-multiline regular expression, alastIndex
value of0
with the sticky flag would be in effect the same as starting the regular expression with^
which requires the match to start at the beginning of the text searched.
这是该博客中的一个示例,其中在 test
方法调用之前操作了 lastIndex
属性,从而强制使用不同的匹配结果:
And here is an example from that blog, where the lastIndex
property is manipulated before the test
method invocation, thus forcing different match results:
var searchStrings, stickyRegexp;
stickyRegexp = /foo/y;
searchStrings = [
"foo",
" foo",
" foo",
];
searchStrings.forEach(function(text, index) {
stickyRegexp.lastIndex = 1;
console.log("found a match at", index, ":", stickyRegexp.test(text));
});
结果:
"found a match at" 0 ":" false
"found a match at" 1 ":" true
"found a match at" 2 ":" false
这篇关于JavaScript RegExps中"y"粘性模式修饰符的作用是什么?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!