I have a string, e.g.:
"The red letters in the following words are suffixes: beauti*ful*, speech*less* and invinc*ible*."
I want to replace the first of each ** pair with <span class='red'> and the second with </span>. I can do this in a for loop, but would like to know how to do it with RegExp.
How about:
s = s.replace(/\*([^*]*)\*/g, "<span class='red'>$1</span>");
\*([^*]*)\* is a little confusing, it searches for:
\* - the first asterisk([^*]*) - the content between the asterisks (captures, so we can replace using $1.\* - the second asteriskWorking example: http://jsbin.com/isufes
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