问题
So I'm trying to replace this:
<h1>My Title <INTRO></h1>
With this:
<h1>My Title <span><INTRO></span></h1>
But...
In a way that dynamically looks for all instances of text wrapped in < ... > and wraps that in span tags.
Something like the code below, but I can't get the regex right.
str = str.replace(/(?<=<)\r\n(?=>)/, "<span>$1</span>");
回答1:
You can use the following regex:
(<[\s\S]*?>)
And replace with:
<span>$1</span>
See DEMO
Js Code:
var str = "<h1>My Title <INTRO></h1>"
str = str.replace(/(<[\s\S]*?>)/, "<span>$1</span>");
alert(str);
来源:https://stackoverflow.com/questions/30852689/javascript-regex-to-look-for-characters-and-wrap-text-with-span-dynamically