I have a string like this:
Free-coffee and coffee-free is free coffee
I need to match and replace only alone word free
but not words {Free}-coffee
and coffee-{free}
Idea is to mark bad words inside string and add HTML tag <strong>
like this:
Free-coffee and coffee-free is <strong>free<strong> coffee.
I try with space but sometimes fail if before this sentence have space.
Here is my current regex:
/(\sfree\s|\sfree|free\s)/ig
NOTE: This need to be case insensitive.
And here is example of code:
var text = "Free-coffee and coffee-free is free coffee";
text = text.replace(/(\sfree\s|\sfree|free\s)/ig, " <strong>strong</strong> ");
Help me please.
Use the following regex pattern:
(^|\s)
- ensures thatfree
word is either at the start of the string OR preceded by space(\s|$)
- ensures thatfree
word is either at the end of the string OR followed by space