I try to solve the same problem in javascript with regexp mentioned here: Check if string is repetition of an unknown substring
I translated the regex in the first answer to Javascript: ^(.+){2,}$
But it does not work as I expect:
'SingleSingleSingle'.replace(/^(.+){2,}$/m, '$1') // returns 'e' instead of exptected 'Single'
What am I overlooking?
I currently have no explanation for why it returns
e, but.matches any character and.{2,}basically just means “match any two or more characters”.What you want is to match whatever you captured in the capture group, by using backreferences:
I just noticed that this is also what the answer you linked to suggests to use:
/(.+)\1+/. The expression is exactly the same, there is nothing you have to change for JavaScript.