Skip to content
Advertisement

Regex for finding only single alphabets in a string and ignore consecutive double

I have searched a lot but I am unable to find a regex that could select only single alphabets and double them while those alphabets which are already double, should remain untouched.

I tried

JavaScript

But since this (\w)\1+ selects all double elements, my output becomes yahoooo. I tried to add negation to it !(\w)\1+ but didn’t work and output becomes same as input. I have tried

JavaScript

But that doubles every character including which are already doubled.

Please help to write an regex that could replace all single character with double while double character should remain untouched.

Example

JavaScript

Advertisement

Answer

You can match using this regex:

JavaScript

And replace it with:

JavaScript

RegEx Demo

RegEx Explanation:

  • ((.)2+): Match a character and capture in group #2 and using 2+ next to it to make sure we match all multiple repeats of captured character. Capture all the repeated characters in group #1
  • |: OR
  • (.): Match any character and capture in group #3

Code Demo:

JavaScript

Output:

JavaScript
User contributions licensed under: CC BY-SA
5 People found this is helpful
Advertisement