yckart yckart - 8 months ago 28
Javascript Question

Get duplicate characters in string

I try to match/get all repetitions in a string. This is what I've done so far:

var str = 'abcabc123123';
var REPEATED_CHARS_REGEX = /(.).*\1/gi;

console.log( str.match(REPEATED_CHARS_REGEX) ); // => ['abca', '1231']

As you can see the matching result is
['abca', '1231']
, but I excpect to get
['abc', '123']
. Any ideas to accomplish that?

2nd question:

Another thing I excpect, is to make it possible to change the duration how often a char needs to be in the string to get matched...

For example if the string is
and the repetation-time is set to
it should result in
. If set to
it should be


A non-
solution is perfectly alright!


Well, I think falsetru had a good idea with a zero-width look-ahead.

// ["abc", "123"]

This allows it to match just the initial substring while ensuring at least 1 repetition follows.

For M42's follow-up example, it could be modified with a .*? to allow for gaps between repetitions.

// ["ab", "12"]

Then, to find where the repetition starts with multiple uses together, a quantifier ({n}) can be added for the capture group:

// ["abcabc"]

Or, to match just the initial with a number of repetitions following, add the quantifier within the look-ahead.

// ["ab"]

It can also match a minimum number of repetitions with a range quantifier without a max -- {2,}

// ["b", "cd", "2", "34"]