JavaScript正则表达式和子匹配 [英] JavaScript regular expressions and sub-matches

查看:114
本文介绍了JavaScript正则表达式和子匹配的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

为什么在设置 g 修饰符时,Javascript子匹配会停止工作?

Why do Javascript sub-matches stop working when the g modifier is set?

var text = 'test test test test';

var result = text.match(/t(e)(s)t/);
// Result: ["test", "e", "s"]

以上工作正常,结果[1] e结果[2] s

The above works fine, result[1] is "e" and result[2] is "s".

var result = text.match(/t(e)(s)t/g);
// Result: ["test", "test", "test", "test"]

以上忽略了我的捕获组。以下是唯一有效的解决方案吗?

The above ignores my capturing groups. Is the following the only valid solution?

var result = text.match(/test/g);
for (var i in result) {
    console.log(result[i].match(/t(e)(s)t/));
}
/* Result:
["test", "e", "s"]
["test", "e", "s"]
["test", "e", "s"]
["test", "e", "s"]
*/


推荐答案

使用字符串匹配()如果设置了全局修改器,函数将不会返回捕获的组。

Using String's match() function won't return captured groups if the global modifier is set, as you found out.

在这种情况下,你会想要使用 RegExp 对象并调用其 exec()函数。 字符串匹配()几乎与 RegExp 相同>的 exec()函数......除非是这样的情况。如果设置了全局修饰符,则正常的 match()函数将不会返回捕获的组,而 RegExp 's exec()函数会。 (注意此处,以及其他地方。)

In this case, you would want to use a RegExp object and call its exec() function. String's match() is almost identical to RegExp's exec() function…except in cases like these. If the global modifier is set, the normal match() function won't return captured groups, while RegExp's exec() function will. (Noted here, among other places.)

要记住的另一个问题是 exec()不会返回一个大数组中的匹配 - 它会一直返回匹配直到它用完,在这种情况下它返回 null

Another catch to remember is that exec() doesn't return the matches in one big array—it keeps returning matches until it runs out, in which case it returns null.

所以,例如,你可以做一些事情像这样:

So, for example, you could do something like this:

var pattern = /t(e)(s)t/g;  // Alternatively, "new RegExp('t(e)(s)t', 'g');"
var match;    

while (match = pattern.exec(text)) {
    // Do something with the match (["test", "e", "s"]) here...
}

另外需要注意的是 RegExp.prototype。 exec() RegExp.prototype.test()在提供的字符串上执行正则表达式并返回第一个结果。每个顺序调用将根据字符串中的当前位置逐步更新结果集 RegExp.prototype.lastIndex

Another thing to note is that RegExp.prototype.exec() and RegExp.prototype.test() execute the regular expression on the provided string and return the first result. Every sequential call will step through the result set updating RegExp.prototype.lastIndex based on the current position in the string.

这是一个例子:
//记住示例和模式中有4个匹配项。 lastIndex从0开始

Here's an example: // remember there are 4 matches in the example and pattern. lastIndex starts at 0

pattern.test(text); // pattern.lastIndex = 4
pattern.test(text); // pattern.lastIndex = 9
pattern.exec(text); // pattern.lastIndex = 14
pattern.exec(text); // pattern.lastIndex = 19

// if we were to call pattern.exec(text) again it would return null and reset the pattern.lastIndex to 0
while (var match = pattern.exec(text)) {
    // never gets run because we already traversed the string
    console.log(match);
}

pattern.test(text); // pattern.lastIndex = 4
pattern.test(text); // pattern.lastIndex = 9

// however we can reset the lastIndex and it will give us the ability to traverse the string from the start again or any specific position in the string
pattern.lastIndex = 0;

while (var match = pattern.exec(text)) {
    // outputs all matches
    console.log(match);
}

您可以找到有关如何使用 RegExp <的信息/ code>对象在MDN上(具体来说,这里是 <$的文档c $ c> exec()功能)。

You can find information on how to use RegExp objects on the MDN (specifically, here's the documentation for the exec() function).

这篇关于JavaScript正则表达式和子匹配的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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