TheWandererLee
TheWandererLee

Reputation: 1032

Match Non-Consecutive Duplicate Characters

For example:

I want to match duplicate characters that are separated by other characters:

I am close, getting the first character of every duplicate by using lookaheads:

Regex101 link

['stress','lambda','moonmen'].forEach( (e) => {
  console.log( e.match(/(.)(?=.*\1)/g) )
} )

But how would I get all duplicate characters?

Upvotes: 1

Views: 1337

Answers (2)

The fourth bird
The fourth bird

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

Emma
Emma

Reputation: 27723

This is pretty complex, my guess is that maybe this expression might be an step closer:

(?:(.)(?=(.*)\1))

DEMO

Upvotes: 1

Related Questions