javascript 当我期望匹配时,match() 返回包含两个匹配的数组
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/9002771/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
match() returns array with two matches when I expect one match
提问by Vasil
Consider the following example:
考虑以下示例:
<html>
<body>
<script type="text/javascript">
var str="filename.jpg";
var pattOne = new RegExp('\.[^\.]*$');
var pattTwo = new RegExp('(\.[^\.]*$)');
var pattThree = new RegExp('(\.[^\.]*$)', 'g');
document.write(str.match(pattOne));
document.write('<br>');
document.write(str.match(pattTwo));
document.write('<br>');
document.write(str.match(pattThree));
</script>
</body>
</html>
Here is the result:
结果如下:
.jpg
.jpg,.jpg
.jpg
I expect this result:
我期待这个结果:
.jpg
.jpg
.jpg
Why placing parenthesis around the regular expression changes the result? Why using 'g' modifier changes again the result?
为什么在正则表达式周围放置括号会改变结果?为什么使用 'g' 修饰符会再次改变结果?
回答by Felix Kling
From String.prototype.match
[MDN]:
来自String.prototype.match
[MDN]:
If the regular expression does not include the
g
flag, returns the same result asregexp.exec(string)
.
如果正则表达式不包含该
g
标志,则返回与regexp.exec(string)
.
Where the RegExp.prototype.exec
documentation [MDN]says:
当RegExp.prototype.exec
文档[MDN]说:
The returned array has the matched text as the first item, and then one item for each capturing parenthesis that matched containing the text that was captured.
返回的数组将匹配的文本作为第一项,然后是每个匹配的捕获括号的一项,其中包含捕获的文本。
So as you introduced a capture group in the second expression, the first element is the whole match and the second contains the content of the capture group, which, in your example, is the whole match as well.
The first expression does not have a capture group, so you only get back the match.
因此,当您在第二个表达式中引入捕获组时,第一个元素是整个匹配项,第二个元素包含捕获组的内容,在您的示例中,它也是整个匹配项。
第一个表达式没有捕获组,因此您只能返回匹配项。
Back to the match
documentation:
回到match
文档:
If the regular expression includes the
g
flag, the method returns an Array containing all matches. If there were no matches, the method returnsnull
.
如果正则表达式包含该
g
标志,则该方法返回一个包含所有匹配项的数组。如果没有匹配项,则该方法返回null
。
With the g
modifier, only matches are returned, but not the content of capture groups. In your string there is only one match.
使用g
修饰符,只返回匹配项,而不返回捕获组的内容。在您的字符串中只有一个匹配项。
回答by Wouter J
The .match()
function returns an array. document.write()
printed the array as a string.
该.match()
函数返回一个数组。document.write()
将数组打印为字符串。
When you captured a group in the string it makes an Array like this:
当您在字符串中捕获一个组时,它会生成一个如下所示的数组:
Array(
[0] => 'the complete matched string',
[1] => 'the first captured group',
[2] => 'the second captured group',
[.] => '...'
)
So with your regex it becomes:
所以你的正则表达式变成了:
Array(
[0] => '.jpg', // You match .jpg of the string
[1] => '.jpg' // You captured the .jpg match
)
And if you print an array it places a ,
between the values.
如果你打印一个数组,它会,
在值之间放置一个。