RegExp构造函数属性输入 [英] RegExp constructor properties input

查看:178
本文介绍了RegExp构造函数属性输入的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

将全局标记设置为true:

Setting the global flag to 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:

Without setting the global flag to 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] - 我对此输出感到满意。

  • 现在我已经与其他字符串构造函数匹配了模式我首先匹配的字符串

  • [3] - 只有通过再次匹配模式与我获得所需结果的字符串。

为什么我必须使用 patt.exec方法两次来更改构造函数属性的更新结果?

Why would i have to use patt.exec method twice to change the update the result by constructor property?

Ques - 只有当全局模式的标志设置为 true 时才会发生。

Ques-It only happens when global flag of pattern is set to true .If global flag is not set the updation of result by constructor property happens the very first time.

推荐答案

<$ c $如果没有设置全局标志,则构造函数属性的结果更新发生在第一次。当给定一个字符串时, RegExp.exec

将:


  • 如果模式是全局的(具有 g 标志) ,那么它将在 RegExp 实例中使用 lastIndex >并从指示的索引中搜索字符串中的模式。 这意味着 RegExp.exec 不知道输入字符串

  • If the pattern is global (has g flag), then it will use lastIndex property in the RegExp instance and search the string for the pattern from the index indicated. This means that RegExp.exec is not aware of the input string. It will just take the index as starting point and search the string, no matter whether the string is the same as the previous call or not.

如果找到一个匹配项,那么它将以索引作为起始点并搜索该字符串,无论该字符串是否与上一次调用相同。它将返回一个具有匹配的数组,并且还会相应地更新 RegExp 实例中的字段,如参考 lastIndex 将更新为开始下一个匹配的位置。

If a match is found, it will return an array with the match, and it also update the fields in the RegExp instance accordingly, as shown in the reference. lastIndex will be updated with the position to start the next match.

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

If a match is not found, it will reset the lastIndex to 0, and return null as result of calling RegExp.exec.

如果模式不是全局的( g flag not set) lastIndex 属性将被忽略。匹配始终从索引0开始,而不管 lastIndex 属性。

If the pattern is not global (g flag not set), lastIndex property will be ignored. The match always starts from index 0 regardless of lastIndex property.

非常清楚:


  • RegExp 将存储开始下一个匹配的位置( lastIndex ),标志的状态(全局 multiline ignorecase )和模式文本( source )。

  • RegExp instance will store the position to start next match (lastIndex), the status of the flags (global, multiline, ignorecase) and the text of the pattern (source).

RegExp.exec 的返回值是一个数组,匹配。数组还具有存储输入字符串和 index 属性的输入属性,它存储基于0的匹配索引

The return value of RegExp.exec is an array that stores the result of the match. The array also have input property which stores the input string and index property which stores the 0-based index of the match.

RegExp。$ _ c>属性和其他几个类似的属性 RegExp object 已弃用。只需通过 RegExp.exec 返回的数组访问它们。 $ _ 等效于 RegExp.exec 返回的数组中附加的输入 / code>。

RegExp.$_ property and several other similar properties on RegExp object are deprecated. Just access them via the array returned by RegExp.exec. $_ is equivalent to the input property attached in the array returned by 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 实例时,它是非常混乱的 - 你不知道属性是从当前还是以前的执行,如在

Since those properties are on the RegExp object, it is very confusing when you work with multiple RegExp instances - you don't know whether the properties are from the current or previous execution, such as in this case.

从您获得的行为看, RegExp。$ _ c $ c> RegExp.exec 找到一个匹配,当 RegExp.exec 匹配失败时,它不会被修改(以前的值保留)。

From the behavior that you get, it seems that RegExp.$_ is modified when RegExp.exec found a match, and it will not be modified (previous value stay) when RegExp.exec fails to match.

请阅读上面的部分,了解其工作原理的全貌。

Please read the part above for full picture of how it works.

我对您的原始代码中的场景后面发生的事情做了一些评论:

I added some comment on what happens behind the scene in your original code:

全局标志

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]

strong>

No global flag

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构造函数属性输入的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆