Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Javascript case-insensitive matching and replacing?

Tags:

javascript

Basically, I need to be able to find certain words (by 'word' I mean a set of characters) in a string (case insensitive) and if they match, I need to insert a symbol after the first letter of that particular set of characters. I can't use search replace, as that would not preserve the case.

Example:

Brown brownies are in an oven.

If the word I'm looking for is brown, and the character I want to insert is *, the result should be:

B*rown b*rownies are in an oven.

What is the best way to do so in JS?

like image 689
Kristina Brooks Avatar asked Aug 08 '11 22:08

Kristina Brooks


1 Answers

Regex with option 'ig' does the trick.

"Brown brownies are in an oven.".replace(/(b)(rown)/gi, "$1*$2")
like image 97
cellcortex Avatar answered Oct 05 '22 19:10

cellcortex