Given a string, var str = "abcc"
, if var regExp = /(.)\1+/
, then it successfully identify "cc" by running "abcc".match(regExp);
, but why /.\1+/
not works since it also means more than one copies of the previous character?
Please provide some insight?
Asked
Active
Viewed 526 times
0

Wiktor Stribiżew
- 607,720
- 39
- 448
- 563

juanli
- 583
- 2
- 7
- 19
-
1https://regexper.com run them through this – Andrew Bone Feb 22 '18 at 14:49
-
1Cool! I just tested the regular expression on it. – juanli Feb 22 '18 at 14:52
1 Answers
2
(.)
denotes a capture group that is later referred to using \1
, so basically you say "find multiple occurrences of capture group 1".
Your second example won't work because there is no capture group involved.

Katajun
- 435
- 3
- 14
-
I want to know why I have to use 'capture group' in this case to make it work. Does '/.\1+/' not indicate the repeated consecutive characters? I don't understand it. – juanli Feb 22 '18 at 14:55
-
-
1Without a capture group, `\1` is meaningless. If removed and left with `/.+/` (or even `/(.)+/`, this would simply mean "match one or more of any character" and would match any string longer than zero characters. – Katajun Feb 22 '18 at 15:04