将全局标志设置为 true:
<script>
var str="I am really puzzled up";
var str1="Being puzzled is first step towards understanding";
var patt=new RegExp("puzzled","gi");
patt.exec(str);
alert(RegExp.$_); //I am really puzzled up *[1]
patt.exec(str1);
alert(RegExp.$_); //I am really puzzled up *[2]
patt.exec(str1);
alert(RegExp.$_); //Being puzzled is first step towards understanding *[3]
</script>
不将全局标志设置为 true:
<script>
var str="I am really puzzled up";
var str1="Being puzzled is first step towards understanding";
var patt=new RegExp("puzzled","i");
patt.exec(str);
alert(RegExp.$_); //I am really puzzled up
patt.exec(str1);
alert(RegExp.$_); //Being puzzled is first step towards understanding
</script>
输出已通过评论显示
- [1]-我对这个输出很满意
- [2]-现在,当我已经将模式与其他一些字符串构造函数属性进行匹配时,仍然显示第一个匹配的字符串
- [3]-只有再次将模式与字符串匹配才能得到所需的结果。
为什么我必须使用patt.exec方法
两次来更改构造函数属性的更新结果?
问题 - 仅当模式的 global
标志设置为 true
时才会发生。如果未设置全局标志,则构造函数属性对结果的更新将在第一次发生.
最佳答案
RegExp.exec
怎么样?有效吗?
当给定一个字符串时,RegExp.exec
将会:
如果模式是全局的(有
g
标志),那么它将使用lastIndex
属性在RegExp
实例并从指示的索引中搜索字符串以查找模式。 这意味着RegExp.exec
不知道输入字符串。它只会以索引为起点搜索字符串,无论字符串是否与上次调用相同。如果找到匹配项,它将返回一个包含匹配项的数组,并且还会更新
RegExp
中的字段。 实例相应地,如 reference 中所示。lastIndex
将更新下一场比赛开始的位置。如果未找到匹配项,则会重置
lastIndex
为 0,并返回null
调用RegExp.exec
的结果.如果模式不是全局的(
g
标志未设置),lastIndex
属性将被忽略。匹配始终从索引 0 开始,无论lastIndex
如何属性(property)。
非常清楚:
RegExp
实例将存储下一场比赛的开始位置(lastIndex
)、标志的状态(global
、multiline
、ignorecase
)以及模式的文本(source
) )。RegExp.exec
的返回值是一个存储匹配结果的数组。该数组还有input
存储输入字符串和index
的属性属性存储匹配的从 0 开始的索引。
RegExp.$_
RegExp
的属性(property)对象
RegExp.$_
RegExp
上的属性(property)和其他几个类似的属性(property)对象 are deprecated 。只需通过 RegExp.exec
返回的数组访问它们。 $_
相当于 input
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
时会非常困惑实例 - 您不知道这些属性是来自当前执行还是先前执行,例如本例。
从您得到的行为来看,似乎 RegExp.$_
被修改时RegExp.exec
找到匹配项,当 RegExp.exec
时不会修改(保留之前的值)匹配失败。
行为说明
请阅读上面的部分以全面了解其工作原理。
我在原始代码中添加了一些关于幕后发生的事情的评论:
全局标志
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]
没有全局标志
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
关于javascript - RegExp 构造函数属性输入,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/16579947/