RegExp构造函数属性输入

RegExp constructor properties input

本文关键字:输入 属性 构造函数 RegExp      更新时间:2023-09-26

将全局标志设置为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 method两次来更改通过构造函数属性更新结果?

Ques只有当模式的global标志设置为true时才会发生。如果没有设置全局标志,则构造函数属性的结果更新将在第一次发生。

RegExp.exec是如何工作的

当给定字符串时,RegExp.exec将:

  • 如果模式是全局的(具有g标志(,则它将在RegExp实例中使用lastIndex属性,并从指示的索引中搜索该模式的字符串这意味着RegExp.exec不知道输入字符串。它只会以索引为起点,搜索字符串,无论字符串是否与上一次调用相同。

    如果找到匹配,它将返回一个具有匹配的数组,并相应地更新RegExp实例中的字段,如参考中所示。lastIndex将被更新为开始下一场比赛的位置。

    如果未找到匹配,则会将lastIndex重置为0,并返回null作为调用RegExp.exec的结果。

  • 如果模式不是全局的(未设置g标志(,则lastIndex属性将被忽略。无论lastIndex属性如何,匹配始终从索引0开始。

非常清楚:

  • RegExp实例将存储开始下一个匹配的位置(lastIndex(、标志的状态(globalmultilineignorecase(和模式的文本(source(。

  • RegExp.exec的返回值是存储匹配结果的数组。该数组还具有存储输入字符串的input属性和存储匹配的基于0的索引的index属性。

RegExp对象的RegExp.$_性质

RegExp.$_属性和RegExp对象上的其他几个类似属性已被弃用。只需通过RegExp.exec返回的数组访问它们。CCD_ 32等效于CCD_ 34返回的数组中附加的CCD_。

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.exec找到匹配时,RegExp.$_似乎被修改了,而当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