Skip to content
Advertisement

Lookahead and Lookbehind validation for each letters in a string (Regular Expression XYX or XXX matching) [closed]

I need to find the letters which have the same letter before and after. Using regular expression how do that?

Example:

Input                                                      ➵   Output
oooh my god                                                ➵    ooo
Llanfairpwllgwyngyllgogerychwyrndrobwllllantysiliogogogoch ➵    gog, lll, lll, ogo, gog, ogo, gog, ogo
Jewnited Snakes of Amerikkka                               ➵    kkk
skilllessness                                              ➵    lll
salpingooophoritis                                         ➵    ooo, iti
princessship / mistressship                                ➵    ssss, sss
same letter / triple letters                               ➵    ele

Basically, I need to find out pattern XXX or XYX, excluding special characters or space.

Advertisement

Answer

Here’s a solution using JavaScript with regex:

(?=((w)w2))

The results are stored in group 1.

const inputs = [
  'oooh my god',
  'Llanfairpwllgwyngyllgogerychwyrndrobwllllantysiliogogogoch',
  'Jewnited Snakes of Amerikkka',
  'skilllessness',
  'salpingooophoritis',
  'princessship / mistressship',
  'same letter / triple letters'
];
  
const results = inputs.map(input => [...input.matchAll(/(?=((w)w2))/g)].map(res => res[1]));
  
console.log(results);

Not sure where does ele come from same letter / triple letters and ssss from princessship / mistressship. I assume those are typos

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