I have a string with underscores around certain words that need replacement with an open and close
html tags.
Heres w
You may use
var str = "_Lorem Ipsum_ is simply dummy text";
var t2 = str.replace(/_([^_]*)_/g, '<i>$1</i>');
console.log(t2);
Details
_
- matches an underscore([^_]*)
- Group 1: zero or more chars other than _
_
- an underscore.The replacement contains a replacement backreference, $1
, that inserts the Group 1 value in between <i>
and </i>
.
See the regex demo.