0

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?

Wiktor Stribiżew
  • 484,719
  • 26
  • 302
  • 397
juanli
  • 517
  • 2
  • 7
  • 14

1 Answers1

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.

Katamari
  • 379
  • 2
  • 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
  • I understand now. Thanks again. – juanli Feb 22 '18 at 15:03
  • 1
    Without 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. – Katamari Feb 22 '18 at 15:04