I have a string, such as hello _there_
. I'd like to replace the two underscores with <div>
and </div>
respectively, using javascript. The output would (therefore) look like hello <div>there</div>
. The string might contain multiple pairs of underscores.
What I am looking for is a way to either run a function on each match, the way ruby does it:
"hello _there_".gsub(/_.*?_/) { |m| "<div>" + m[1..-2] + "</div>" }
Or be able to reference a matched group, again the way it can be done in ruby:
"hello _there_".gsub(/_(.*?)_/, "<div>\\1</div>")
Any ideas or suggestions?