Random insensitive javascript replaces regex based on word boundary

I am writing a replace function in javascript taking an account in word boundary

blog = blog.toLowerCase().replace(new RegExp("\\b" + wordList[i].toLowerCase() + "\\b", 'g'), "value to replace"); 

Now it is CASE SESITIVE Replace. I want to make it CASE INSENSITIVE .

How can i do this?

Although for case insensitive there \ I exist, but I don’t know how to put it in my code

Any help is appreciated.

+1
source share
2 answers

Just pass i with g in the flags argument of the new RegExp() constructor . For instance:

 new RegExp("\\b" + wordList[i].toLowerCase() + "\\b", 'gi') 

At this point, you can delete all calls to String.toLowerCase :

 var re = new RegExp("\\b" + wordList[i] + "\\b", 'gi'); blog = blog.replace(re, 'value to replace'); 

NB you may need to avoid the value of worldList\[i\] so that your code does not accidentally try to create an invalid regular expression.

+3
source

change your 'g' to 'gi' - "g" means "global", "i" means "case insensitive":

 ..., 'gi'), "value... 
+2
source

Source: https://habr.com/ru/post/1486365/


All Articles