2

How with help of RegExp in JS delete word from line that include two similar symbols one after another. For example we have line "Hello, Aleks!". We need to delete "Hello" because we have "ll". I can use alert("Hello, Aleks!".replace(/(.)\1/g, "$1")). It will delete double letters. But I need to delete words that include double letters. Thank you!

Aleks
  • 45
  • 4

1 Answers1

1

You may use \w* (0 or more letters/digits/_ symbols) before an after the doubled letter:

var res = "Hello, Aleks!".replace(/\w*(\w)\1\w*/g, "");
console.log(res);

To also remove any non-word chars to follow, add \W* (0 or more chars other than letters/digits/_ symbols):

var res = "Hello, Aleks!".replace(/\w*(\w)\1\w*\W*/g, "");
console.log(res);

Note: \w matches letters, digits and _. If you want to only match letters, use [a-zA-Z] instead.

If you need to match Unicode letters, either use XRegExp \p{L}, or build a regex from Unicode ranges.

Community
  • 1
  • 1
Wiktor Stribiżew
  • 607,720
  • 39
  • 448
  • 563