javascript - RegExp 构造函数属性输入

标签 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(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 )、标志的状态( globalmultilineignorecase )以及模式的文本( 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/

相关文章:

python - 如何修改此正则表达式以不匹配不间断空格?

javascript - 如何从javascript中的字符串中找出电子邮件和姓名

java - 披萨订购计划

c++ - 为什么在删除 `copy constructor`后仍使用 `move constructor`?

javascript - 如何使用PyV8和httplib解释javascript

html - 编码 HTML 的正则表达式

javascript - 排序在智能表 Angular js中不起作用

c++ - 我可以在父构造函数中多次重用函数的返回值吗?

javascript - jquery 解决方案/javascript 函数分配变量

javascript - 如何将多个 Canvas 元素放入一个 Canvas 元素中?