Get duplicate characters in string

后端 未结 3 1251
一向
一向 2020-12-22 06:36

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         


        
3条回答
  •  盖世英雄少女心
    2020-12-22 07:15

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

    'abcabc123123'.match(/(.+)(?=\1)/g)
    // ["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.

    'abc123ab12'.match(/(.+)(?=.*?\1)/g)
    // ["ab", "12"]
    

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

    'abcabc1234abc'.match(/(.+){2}(?=.*?\1)/g)
    // ["abcabc"]
    

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

    'abc123ab12ab'.match(/(.+)(?=(.*?\1){2})/g)
    // ["ab"]
    

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

    'abcd1234ab12cd34bcd234'.match(/(.+)(?=(.*?\1){2,})/g)
    // ["b", "cd", "2", "34"]
    

提交回复
热议问题