获取字符串中的重复字符 [英] Get duplicate characters in string

查看:198
本文介绍了获取字符串中的重复字符的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我尝试匹配/获取字符串中的所有重复。这是我到目前为止所做的:

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

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

正如你可以看到的匹配结果是 ['abca','1231'] ,但我可以得到 ['abc','123']



,是可以更改字符串中需要多长时间获得匹配的持续时间 ...



例如如果字符串 abcabcabc 重复时间设置为 2 ['abcabc'] 中。如果设置为 3 ,则应为 ['abc']


$ b b

更新



非 - RegExp / p>

解决方案

好吧,我想 falsetru 有一个好主意,零宽度前瞻。

 'abcabc123123'.match(/(.+ )(?= \1)/ g)
// [abc,123]

这允许它只匹​​配初始子字符串,同时确保至少重复1次。



对于 M42 的后续示例中,可以使用。*?修改以允许重复之间的间隙。 / p>

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

然后,可以为捕获组添加量词( {n} ):

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

或者,为了仅匹配初始值与后面的重复次数,请在预测内添加量词。

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

它也可以匹配最小重复次数不带max的范围量词符 - {2,}

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


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 abcabcabc and the repetation-time is set to 2 it should result in ['abcabc']. If set to 3 it should be ['abc'].

Update

A non-RegExp solution is perfectly alright!

解决方案

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"]

这篇关于获取字符串中的重复字符的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆