我无法准确理解JavaScript的方法string.match(regexp)的g标志是如何工作的 [英] I can't accurately understand how does JavaScript's method string.match(regexp)'s g flag work

查看:107
本文介绍了我无法准确理解JavaScript的方法string.match(regexp)的g标志是如何工作的的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在JavaScript:The Good Parts一书中,它解释了方法 string.match(regexp),如下所示:

In the book "JavaScript: The Good Parts", it explains method string.match(regexp) as below:


匹配方法匹配字符串和正则表达式。如何
这取决于g标志。如果没有g标志,那么调用string .match(regexp)的
结果与调用
regexp .exec(string)相同。但是,如果正则表达式具有g标志,那么
会生成所有匹配的数组但不包括捕获
组:

The match method matches a string and a regular expression. How it does this depends on the g flag. If there is no g flag, then the result of calling string .match( regexp ) is the same as calling regexp .exec( string ). However, if the regexp has the g flag, then it produces an array of all the matches but excludes the capturing groups:

然后这本书提供了代码示例:

Then the book provides code example:

var text = '<html><body bgcolor=linen><p>This is <b>bold<\/b>!<\/p><\/body><\/html>';
var tags = /[^<>]+|<(\/?)([A-Za-z]+)([^<>]*)>/g;
var a, i;
a = text.match(tags);
for (i = 0; i < a.length; i += 1) {
    document.writeln(('// [' + i + '] ' + a[i]).entityify());
}
// The result is
// [0] <html>
// [1] <body bgcolor=linen>
// [2] <p>
// [3] This is
// [4] <b>
// [5] bold
// [6] </b>
// [7] !
// [8] </p>
// [9] </body>
// [10] </html>

我的问题是我无法理解但不包括捕获组。

My question is that I can't understand "but excludes the capturing groups".

在上面的代码示例中,< / html> html >在捕获组中。为什么它仍然包含在结果数组中?

In the code example above, html in the </html> is in a capturing group. And why is it still included in the result array?

/ < ; / html> 也在捕获组中。为什么它包含在结果数组中?

And / in the </html> is also in a capturing group. And why is it included in the result array?

你能用上面的代码示例解释但是排除捕获组吗?

Could you explain "but excludes the capturing groups" with the code example above?

非常感谢!

推荐答案


在上面的代码示例中,html在一个捕获组中。为什么它仍然包含在结果数组中?

In the code example above, html in the is in a capturing group. And why is it still included in the result array?

因为它是完全匹配。当他说但不包括捕获组时,他并不意味着完全匹配结果,只是捕获组的内容不是在数组中重复。如果包含了捕获组,你会看到

Because it's the full match. When he says "but excludes the capture groups" he doesn't mean from the full match result, just that the contents of the capture groups aren't reiterated in the array. If the capturing groups were included, you'd see

// The result is
// [0] <html>
// [1]           // From the capture group; nothing here
// [2] html      // From the capture group
// [3]           // From the capture group; nothing here
// ...




和/这也是一个捕获组。为什么它包含在结果数组中?

And / in the is also in a capturing group. And why is it included in the result array?

出于同样的原因:它是整体匹配的一部分,那是什么的在结果中;个别捕获组的内容不是。

For the same reason as above: It's part of the overall match, and that's what's in the result; the contents of the individual capture groups are not.

使用更简单的示例更容易理解。考虑以下代码:

This is easier to understand with a simpler example. Consider this code:

var s = "test1 test2";
var re = /(test)(.)/g;
var r = s.match(re);
var i;
for (i = 0; i < r.length; ++i) {
    console.log("[" + i + "]: '" + r[i] + "'");
}

因为正则表达式具有 g flag,只有完整的匹配包含在数组中,所以我们看到:

Because the regular expression has the g flag, only the full matches are included in the array, so we see:

[0]: 'test1'
[1]: 'test2'

在每种情况下,数组中的条目都是完全匹配,其中包括组成捕获组的匹配字符整体表达。

In each case, the entry in the array is the full match, which includes the characters that matched within capture groups making up the overall expression.

如果我们删除 g 标志但没有改变其他任何内容,我们会得到第一次完整匹配,然后是两个捕获组的内容:

If we removed the g flag but didn't change anything else, we'd get the first full match followed by the contents of the two capture groups:

[0]: 'test1'    // The full match, including the stuff from each capture group
[1]: 'test'     // Capture group 0's contents
[2]: '1'        // Capture group 1's contents

在那里,第一个条目是完全匹配;然后第二个和第三个是捕获组的内容。请注意捕获的内容

There, the first entry is the full match; then the second and third are the contents of the capture groups. Note that the contents of the capture gruops

这篇关于我无法准确理解JavaScript的方法string.match(regexp)的g标志是如何工作的的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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