在JavaScript中匹配Vs exec [英] match Vs exec in JavaScript

查看:99
本文介绍了在JavaScript中匹配Vs exec的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我需要对JavaScript中的匹配Vs exec进行一些澄清; 此处有人说

I need some clarification for match Vs exec in JavaScript; here some one says that

带有全局正则表达式的exec意味着在循环中使用但首先如你所见在我的例子中,情况并非如此;在我的示例exec中使用全局正则表达式返回数组中的所有匹配项!其次他们说对于String.match它返回所有匹配而不需要循环!但是再一次,我的例子中没有发生这种情况,只是返回输入字符串?我误解了/做错了什么?

"exec with a global regular expression is meant to be used in a loop" but first of all as you see in my example this is not the case; in my example exec with global regular expression is returning all of the matches in an array! Secondly they say that for String.match it returns all of the matches with no need of looping through! But again that's not happening in my example and it is just returning the input string? Have I misunderstood/done something wrong?

var myString = "[22].[44].[33].";
var myRegexp = /.*\[(\d*)*\].*\[(\d*)*\].*\[(\d*)*\].*/g;

var execResult = myRegexp.exec(myString);
console.log(execResult.length);
console.log(execResult[1]);// returns 22 and execResult has all of my matches from index 1 to the length of array


var matchResult = myString.match(myRegexp);
console.log(matchResult.length);
console.log(matchResult);// returns just myString which is "[22].[44].[33]."! Why is that?


推荐答案


  1. string.match 找到第一个匹配并返回实际匹配,找到文本的索引和实际输入,当不使用全局标志时。

  1. string.match finds the first match and returns it with the actual match, the index at which the text was found and the actual input, when the global flag is not used.

string.match 只返回所有匹配项,时使用全局标志。

string.match just returns all the matches, when the global flag is used.

var myString = "[22].[44].[33].";

console.log(myString.match(/\d+/));
# [ '22', index: 1, input: '[22].[44].[33].' ]
console.log(myString.match(/\d+/g));
# [ '22', '44', '33' ]


string.match regex.exec 之间的主要区别是,正则表达式对象将使用 regex.exec 调用更新当前匹配。例如,

The main difference between string.match and regex.exec is, the regex object will be updated of the current match with regex.exec call. For example,

var myString = "[22].[44].[33].", myRegexp = /\d+/g, result;

while (result = myRegexp.exec(myString)) {
    console.log(result, myRegexp.lastIndex);
}

将返回

[ '22', index: 1, input: '[22].[44].[33].' ] 3
[ '44', index: 6, input: '[22].[44].[33].' ] 8
[ '33', index: 11, input: '[22].[44].[33].' ] 13

如您所见, lastIndex 属性是每当找到匹配项时都会更新。因此,当您使用 exec 时要记住两件事,否则会遇到无限循环。

As you can see, the lastIndex property is updated whenever a match is found. So, keep two things in mind when you use exec, or you will run into an infinite loop.


  1. 如果您不使用 g 选项,那么您将始终获得第一场比赛,如果有的话一,否则 null 。因此,以下内容会遇到无限循环。

  1. If you don't use g option, then you will always get the first match, if there is one, otherwise null. So, the following will run into an infinite loop.

var myString = "[22].[44].[33].", myRegexp = /\d+/, result;

while (result = myRegexp.exec(myString)) {
    console.log(result, myRegexp.lastIndex);
}


  • 不要忘记随后使用相同的正则表达式对象调用。因为,每次更新正则表达式对象,如果你传递新对象,程序将再次进入无限循环。

  • Don't forget to use the same regular expression object with subsequent calls. Because, the regex object is updated every time, and if you pass new object, again the program will run into an infinite loop.

    var myString = "[22].[44].[33].", result;
    
    while (result = /\d+/g.exec(myString)) {
        console.log(result);
    }
    


  • 这篇关于在JavaScript中匹配Vs exec的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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