Home > Software design >  JavaScript Regex to find repeating substring and what is the difference with `/(\w)\1 /g`and`/(\w
JavaScript Regex to find repeating substring and what is the difference with `/(\w)\1 /g`and`/(\w

Time:01-12

I'm trying to use Regex to find the longest repeating substring in a string. for example : when the input is "aabbccc", the expected output should be "ccc" (repeat 3 times) I finally get the answer by using str.match(/(\w)\1 /g). so when const str = 'aabbccc' and run str.match(/(\w)\1 /g) the output will be ['aa', 'bb', 'ccc']

but I was using str.match(/(\w\1 )/g) and the output is ['a', 'a', 'b', 'b', 'c', 'c', 'c'] can someone explain why?

i thought : \w:any word , \1 :repeat one or more than one time \w\1 :any word repeat one or more than one time (\w\1 ): capture any repeat words as a group

CodePudding user response:

(\w) matches a word character for the first capturing group, and \1 matches one of more occurrences of that first capturing group. This finds repeated characters.

In (\w\1 ), the \1 is inside the first capturing group, so it attempts to match one or more occurrences of the group it is contained in.

CodePudding user response:

1 . /(\w)\1 /g uses two sets of parentheses:

(\w), which take any word character and create a group .

\1 is a backreference to the first capture group(\w) and matches one or more repetitions of that captured character. quantifier means "one or more". This expression matches all repeating substrings in a string and return an array of strings.

  1. /(\w\1 )/g use 2 sets of parentheses,

(\w) captures a single word character and 2nd one \1 match one or more same character as the last capture, but this one is not create a group and in this matched repeating characters as individual characters in array.

  • Related