Javascript RegExp构造函数属性输入

Javascript RegExp构造函数属性输入,javascript,regex,constructor,exec,Javascript,Regex,Constructor,Exec,将全局标志设置为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

将全局标志设置为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>
<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>

var str=“我真的很困惑”;
var str1=“困惑是理解的第一步”;
var patt=新的RegExp(“困惑”、“gi”);
副执行官(str);
警报(RegExp.$)//我真的很困惑*[1]
副执行官(str1);
警报(RegExp.$)//我真的很困惑*[2]
副执行官(str1);
警报(RegExp.$)//困惑是理解的第一步*[3]
在不将全局标志设置为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>
<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>

var str=“我真的很困惑”;
var str1=“困惑是理解的第一步”;
var patt=新的RegExp(“困惑的”、“i”);
副执行官(str);
警报(RegExp.$)//我真的很困惑
副执行官(str1);
警报(RegExp.$)//困惑是理解的第一步
已通过注释显示输出

  • [1] -我对这一产出感到满意
  • [2] -现在,当我已经将该模式与其他字符串构造函数匹配时,属性仍然显示第一个匹配的字符串
  • [3] -只有再次将模式与字符串匹配,我才能获得所需的结果
为什么我必须使用
patt.exec方法
两次才能更改“按构造函数更新结果”属性

问:只有当模式的
global
标志设置为
true
时,才会发生这种情况。如果未设置global标志,则第一次按构造函数属性更新结果。

RegExp.exec如何工作? 当给定字符串时,
RegExp.exec
将:

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

    如果找到匹配项,它将返回一个带有匹配项的数组,并相应地更新
    RegExp
    实例中的字段,如中所示
    lastIndex
    将使用开始下一次匹配的位置进行更新

    如果未找到匹配项,它将把
    lastIndex
    重置为0,并在调用
    RegExp.exec
    后返回
    null

  • var arr = pattern.exec(inputString);
    if (arr !== null) {
        // Print to the console the whole input string that has a match
        console.log(arr.input); 
    }
    
  • 如果模式不是全局模式(
    g
    未设置标志),
    lastIndex
    属性将被忽略。无论
    lastIndex
    属性如何,匹配始终从索引0开始

非常清楚地说:

  • RegExp
    实例将存储开始下一次匹配的位置(
    lastIndex
    ),标志的状态(
    global
    multiline
    ignorecase
    )和模式的文本(
    source

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

RegExp
对象的
RegExp.$\ucode>属性
RegExp.$\ucode>属性和
RegExp
对象上的其他几个类似属性。只需通过
RegExp.exec
返回的数组访问它们
$\uu0
相当于
RegExp.exec
返回的数组中附加的
input
属性

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.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
无全局标志

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
RegExp.exec
是如何工作的? 当给定字符串时,
RegExp.exec
将:

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

    如果找到匹配项,它将返回一个带有匹配项的数组,并相应地更新
    RegExp
    实例中的字段,如中所示
    lastIndex
    将使用开始下一次匹配的位置进行更新

    如果未找到匹配项,它将把
    lastIndex
    重置为0,并在调用
    RegExp.exec
    后返回
    null

  • var arr = pattern.exec(inputString);
    if (arr !== null) {
        // Print to the console the whole input string that has a match
        console.log(arr.input); 
    }
    
  • 如果模式不是全局模式(
    g
    未设置标志),
    lastIndex
    属性将被忽略。无论
    lastIndex
    属性如何,匹配始终从索引0开始

非常清楚地说:

  • RegExp
    实例将存储开始下一次匹配的位置(
    lastIndex
    ),标志的状态(
    global
    multiline
    ignorecase
    )和模式的文本(
    source

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