Reputation: 1032
For example:
I want to match duplicate characters that are separated by other characters:
stress
should return sss
lambda
should return aa
moonmen
should return moonmn
I am close, getting the first character of every duplicate by using lookaheads:
['stress','lambda','moonmen'].forEach( (e) => {
console.log( e.match(/(.)(?=.*\1)/g) )
} )
But how would I get all duplicate characters?
Upvotes: 1
Views: 1337
Reputation: 163277
Your pattern matches the latest character that has a duplicate.
As an alternative, knowing that they have a duplicate, you could use a negated character class to remove all the non duplicates.
let pattern = /(.)(?=.*\1)/g;
[
"stress",
"lambda",
"moonmen"
].forEach(s => {
let regex = new RegExp("[^" + [...new Set(s.match(pattern))].join('') + "]+", "g");
console.log(s.replace(regex, ''));
});
If you want to account for special characters in the string, you might use the function on this page to escape characters with special meaning.
Upvotes: 3