【问题标题】:RegExp constructor properties inputRegExp 构造函数属性输入
【发布时间】:2013-05-16 05:56:28
【问题描述】:

将全局标志设置为真:

<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>

输出已由 cmets 显示

  • [1]-我对这个输出很满意
  • [2]-现在,当我已经将模式与其他一些字符串构造函数属性匹配时,仍然显示我第一个匹配的字符串
  • [3]-只有再次将模式与字符串匹配,我才能得到想要的结果。

为什么我必须使用patt.exec method 两次才能通过构造函数属性更改更新结果?

问题-仅当模式的 global 标志设置为 true 时才会发生。如果未设置全局标志,则构造函数属性的结果更新是第一次发生。

【问题讨论】:

  • 好的,但它仍然向我显示了全局标志集的输出(不正确的一个)......并且显示了非全局标志的正确输出。
  • 先生在那一页上写着他们将来会被完全弃用。目前可以使用
  • Deprecated 表示如果您已经在旧代码中使用它,建议您远离它/不要在新代码中使用它。

标签: javascript regex constructor exec


【解决方案1】:

RegExp.exec 是如何工作的?

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

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

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

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

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

非常清楚:

  • RegExp instance 将存储开始下一场比赛的位置 (lastIndex)、标志的状态 (globalmultilineignorecase) 和模式的文本 (source)。

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

RegExpRegExp.$_ 属性对象

RegExp.$_ 属性和RegExp 上的其他几个类似属性 object 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

【讨论】:

  • 我认为没有比这更好的解释了。非常感谢先生。好吧,我认为您当然应该为此获得更多有用的答案...
猜你喜欢
  • 2015-09-02
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2022-12-16
  • 1970-01-01
  • 1970-01-01
  • 2023-04-02
相关资源
最近更新 更多