admin管理员组文章数量:1391987
I am working on this regex expression and i have confusion how it is working.
/^(\d+)\s\1\s\1$/
i know that
^ is start of line
() shows capture group
\d+ shows digits
\s is whitespace
\1 shows to match first group
$ the end
confusing part is $ here
and why it is matching to "40 40 40 "
but not "40 40 40 40"
and role of $
I am working on this regex expression and i have confusion how it is working.
/^(\d+)\s\1\s\1$/
i know that
^ is start of line
() shows capture group
\d+ shows digits
\s is whitespace
\1 shows to match first group
$ the end
confusing part is $ here
and why it is matching to "40 40 40 "
but not "40 40 40 40"
and role of $
-
1
Hint: How many
\1
instances are there? Could you make it match by adding more? – tadman Commented Jan 23, 2020 at 4:09 - two instance because i needed to match three groups not 4 . i am having issues how it works :) – Maria Commented Jan 23, 2020 at 4:13
- @Amadan are you saying $ is "nothing else " here – Maria Commented Jan 23, 2020 at 4:14
-
Pretty much, yes. As you said, it is "end of string" (or "end of line", depending on the
m
flag), and^
is "beginning of string" (or line). The pattern says there shall be three identical things sandwiched from start to end of string, with some spaces between them. If you omitted$
, then"40 40 40foo"
would find a match. If you omitted^
, thenfoo40 40 40"
would. If you omit both,"foo40 40 40foo"
would be okay. If you want three groups, and"40 40 40 40"
is being rejected, then it's all as it should be, no? – Amadan Commented Jan 23, 2020 at 4:18 - pletely got it now usage of ^ and $ was confusing for me now i got it thank You .it is all .it is a challenge in FCC . you are awesome – Maria Commented Jan 23, 2020 at 4:23
3 Answers
Reset to default 6\1
refers to the first capturing group in the regular expression. \2
will refer to the second capturing group and \n will refer to an nth capturing group.
In the regular expression /^(\d+)\s\1\s\1$/
. \1
will match the only group of digits which is matched by the first capturing group (\d+)
.
$
in /^(\d+)\s\1\s\1$/
Means it should end by one more digits.
^
in /^(\d+)\s\1\s\1$/
Means it should start by one more digits.
$
matches "end of string". The way you have the regex pattern defined, it shouldn't match either of your strings because 40 40 40
has an extra space at the end before the end of string and 40 40 40 40
has extra space and digits after the third 40
and before the end of string.
If you remove the $
, then it will match both of your strings or any string that begins with 40 40 40
, so for example it will also match 40 40 40 test
.
I wrote the answer for this challenge as /^(\d+)(\s)\1\2\1$/g and it worked. If I'm correct, the first \1 will produce (number, space, number). The \2 will add the space making it (number, space, number, space). The final \1 adds the last number, making it (number\s number\s number). The ^ and $ start and end the regex.
本文标签: javascriptRegex expression with 1 references not matching desired stringStack Overflow
版权声明:本文标题:javascript - Regex expression with 1 references not matching desired string - Stack Overflow 内容由网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://www.betaflare.com/web/1744697211a2620351.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
发表评论