[英]RegEx not working as expected /(d).\1/
I am beginner in RegEx so I am reading the info page of regEx on stackoverflow. 我是RegEx的初学者,因此我正在stackoverflow上阅读regEx的信息页。
eg: /(d).\\1/ matches and captures 'dad' in "abcdadef" while /(?:.d){2}/ matches but doesn't capture 'cdad'.
例如:/(d).\\1/匹配并捕获“ abcdadef”中的“ dad”,而/(?:.d){2}/匹配但不捕获“ cdad”。
I tried :- 我试过了 :-
var pattern=/(d).\1/
var val="abcdadef";
console.log(pattern.exec(val));
It shows array of ["dad","d"] but i don't know why ? 它显示[[dad],“ d”]的数组,但我不知道为什么? As said in info it just only capture the "dad" why it is capturing two values in array?.
如信息中所述,它仅捕获“爸爸”,为什么要捕获数组中的两个值? And what is the use of '\\1' in the end of pattern ?
在模式结尾使用'\\ 1'有什么用?
Please provide me more info how to use it. 请向我提供更多有关如何使用它的信息。
Thanks :-) 谢谢 :-)
when you use (), you're telling regex to match the in between () and store it as a capturing group. 当您使用()时,您是在告诉正则表达式匹配()之间的内容并将其存储为捕获组。 Each match will have capturing groups of its own.
每个比赛都有自己的捕捉组。 Try your expression here .
在这里尝试表达。 A regex match object is normally a collection that contains the entire match of the regex followed by capturing groups of that match.
regex匹配对象通常是一个集合,其中包含regex的整个匹配项,然后捕获该匹配项的组。
Edit: As per your comment below, here's an another pattern (m).\\1
and the text upon which we're executing the regex is mum. 编辑:根据您在下面的评论,这是另一个模式
(m).\\1
,执行正则表达式的文本是mum。 In this example, regex will attempt to do the following: 在此示例中,正则表达式将尝试执行以下操作:
m
and hence we used ()
, it's going to store the match in a capturing group. m
,因此我们使用()
,它将匹配存储在捕获组中。 This capturing group will make it to the match collection later. .
will match any character other than newline so in our case, it will match the literal u
. u
。 \\1
will attempt to match the next character using the first matching group as a pattern and that would be the literal m
in our case. \\1
将尝试使用第一个匹配组作为模式来匹配下一个字符,在本例中为原义m
。 The final result will be the regex match of mum
and the only capturing group would be m
. 最终结果将是
mum
的正则表达式匹配,唯一的捕获组将是m
。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.