可能重复:
替换字符串中的单词,但忽略HTML
调用Replace时是否可以忽略HTML元素?
示例代码:
$myText.replace(new RegExp( $searchString, 'gi' ),
'<span class="highlight">'+ $searchString + '</span>');
Run Code Online (Sandbox Code Playgroud)
$myText 是一个大的HTML字符串,例如:
var $myText =
"<p>Lorem Ipsum is simply dummy text of the printing and typesetting " +
"industry. Lorem Ipsum has been the industry's standard dummy text " +
"ever since the 1500s, <img src="something">when an unknown printer " +
"took a galley of type and scrambled it to make a type specimen book. " +
"It has survived not only five centuries, " + …Run Code Online (Sandbox Code Playgroud)