X
X
xlo2402018-08-03 13:38:00
JavaScript
xlo240, 2018-08-03 13:38:00

How to replace repeated words in a text with a reg expression?

How to replace repeated words in text with regular expression?
Found a code that removes duplicate words

var str='При при проверке были были найдены повторяющиеся слова';
alert(str.replace(/([^a-zА-яёЁ]|^)([a-zА-яёЁ]+) (\2)([^a-zA-ZА-яёЁ]|$)/gi,"$1"+"$2"+"$4"))

It is necessary to replace repeated words with the same thing but with formatting.
For example
, replace "at" with
<strong>при</strong>

Answer the question

In order to leave comments, you need to log in

1 answer(s)
0
0xD34F, 2018-08-03
@xlo240

It is not very clear with the replacement - is it necessary to remove repetitions? If yes:
If not:

str.replace(/([^a-zА-яёЁ]|^)([a-zА-яёЁ]+) (\2)([^a-zA-ZА-яёЁ]|$)/gi, "$1$2 <strong>$3</strong>$4")

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question