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?
Regex with option 'ig' does the trick.
"Brown brownies are in an oven.".replace(/(b)(rown)/gi, "$1*$2")
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With