Home > other >  Emphasize the part of a word that is not given
Emphasize the part of a word that is not given

Time:11-04

I have the code:

var word = 'thisistheword';
var string = 'istheword';
console.log(word.replace(new RegExp(string, "gi"), "<b>" string "</b>"));

And now I get this<b>istheword</b>, but I want to receive inverted result, so: <b>this</b>istheword.

CodePudding user response:

var word = 'thisistheword';
var string = 'istheword';
console.log(word.replace(new RegExp("(.*)"   string , "gi"), "<b>$1</b>"   string));

gives: <b>this</b>istheword

"(.*)" string means match everything preceding string. Wrapped in () so that you can refer to it later with the $1

  • Related