The task is as follows:

There is an input text, for example: Тест ,слово. Тест.Слово,тест, слово. Тест ,слово. Тест.Слово,тест, слово. .

It is required to find the words inside the text, for example: Тест and mark them with some html tag. At the exit I have to get the text with the words labeled тест case insensitive. The output string should look something like this: <string>Тест</string> ,слово. <string>Тест</string>.Слово,<string>тест</string>, слово. <string>Тест</string> ,слово. <string>Тест</string>.Слово,<string>тест</string>, слово. .

There is an idea to bring the text to lowerCase or upperCase , and search for each word position in the text ( indexOf ) plus the length of the word and wrap these characters in the html tag, but I think this is a crutch option and there is no certainty that it will work.

    2 answers 2

    You can use regular expressions . As parameters passing g - search the entire line (globally) and i - ignore case. Then, using replace replace everything that was chosen by the regular schedule with the desired construction.

     var string = "Тест ,слово. Тест.Слово,тест, слово."; var reg = new RegExp('тест', 'gi'); var result = string.replace(reg, function(str) { return '<strong>' + str + '</strong>' }); console.log(result); 

      Use regular expressions. In the replacement string, use $& to refer to the matching string. The i flag indicates case-insensitive search, the g flag indicates search greed, or a full-line search.

       var input = "Тест ,слово. Тест.Слово,тест, слово.."; var output = input.replace(/тест/ig, "<string>$&</string>");