问题描述
将全局标记设置为true:
Setting the global flag to true:
Ques-It only happens when 的结果返回 null code>。
If a match is not found, it will reset the lastIndex to 0, and return null as result of calling RegExp.exec.
如果模式不是全局的( g flag not set) lastIndex 属性将被忽略。匹配始终从索引0开始,而不管 lastIndex 属性。
If the pattern is not global (g flag not set), lastIndex property will be ignored. The match always starts from index 0 regardless of lastIndex property.
非常清楚:
-
RegExp 将存储开始下一个匹配的位置( lastIndex ),标志的状态(全局 multiline , ignorecase )和模式文本( source )。
RegExp instance will store the position to start next match (lastIndex), the status of the flags (global, multiline, ignorecase) and the text of the pattern (source).
RegExp.exec 的返回值是一个数组,匹配。数组还具有存储输入字符串和 index 属性的输入属性,它存储基于0的匹配索引
The return value of RegExp.exec is an array that stores the result of the match. The array also have input property which stores the input string and index property which stores the 0-based index of the match.
RegExp。$ _ c>属性和其他几个类似的属性 RegExp object 。只需通过 RegExp.exec 返回的数组访问它们。 $ _ 等效于 RegExp.exec 返回的数组中附加的输入 / code>。
RegExp.$_ property and several other similar properties on RegExp object are deprecated. Just access them via the array returned by RegExp.exec. $_ is equivalent to the input property attached in the array returned by RegExp.exec.
var arr = pattern.exec(inputString); if (arr !== null) { // Print to the console the whole input string that has a match console.log(arr.input); }
由于这些属性位于 RegExp 对象,当你使用多个 RegExp 实例时,它是非常混乱的 - 你不知道属性是从当前还是以前的执行,如在
Since those properties are on the RegExp object, it is very confusing when you work with multiple RegExp instances - you don't know whether the properties are from the current or previous execution, such as in this case.
从您获得的行为看, RegExp。$ _ c $ c> RegExp.exec 找到一个匹配,当 RegExp.exec 匹配失败时,它不会被修改(以前的值保留)。
From the behavior that you get, it seems that RegExp.$_ is modified when RegExp.exec found a match, and it will not be modified (previous value stay) when RegExp.exec fails to match.
请阅读上面的部分,了解其工作原理的全貌。
Please read the part above for full picture of how it works.
我对您的原始代码中的场景后面发生的事情做了一些评论:
I added some comment on what happens behind the scene in your original code:
全局标志
var str="I am really puzzled up"; var str1="Being puzzled is first step towards understanding"; // Global pattern var patt=new RegExp("puzzled","gi"); // From index 0 of str, found match at index 12 // RegExp.$_ is set to current input - str // patt.lastIndex is set to index 19 patt.exec(str); alert(RegExp.$_); //I am really puzzled up *[1] // From index 19 of str1, can't find any match // Since no match is found, RegExp.$_'s value is not changed // patt.lastIndex is set to 0 patt.exec(str1); alert(RegExp.$_); //I am really puzzled up *[2] // Found index 0 of str1, found match at index 6 // RegExp.$_ is set to current input - str1 // patt.lastIndex is set to 13 patt.exec(str1); alert(RegExp.$_); //Being puzzled is first step towards understanding *[3]
strong>
No global flag
var str="I am really puzzled up"; var str1="Being puzzled is first step towards understanding"; // Not global var patt=new RegExp("puzzled","i"); // From index 0 of str, found match at index 12 // RegExp.$_ is set to current input - str patt.exec(str); alert(RegExp.$_); //I am really puzzled up // From index 0 of str1, found match at index 6 // RegExp.$_ is set to current input - str1 patt.exec(str1); alert(RegExp.$_); //Being puzzled is first step towards understanding
这篇关于RegExp构造函数属性输入的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!