在使用jQuery替换时,是否可以忽略字符串中的HTML?

可能重复:
替换字符串中的单词,但忽略HTML

调用Replace时是否可以忽略HTML元素?

示例代码:

$myText.replace(new RegExp( $searchString, 'gi' ), ''+ $searchString + ''); 

$myText是一个大的HTML字符串,例如:

 var $myText = "

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, 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, " + "but also the leap into electronic " + "typesetting, remaining essentially unchanged. It was popularised in " + "the 1960s with the release of Letraset sheets containing Lorem Ipsum " + "passages, and more recently with desktop publishing software like " + "Aldus PageMaker including versions of Lorem Ipsum.

"

$searchString等于用户键入文本输入框的任何内容。

如果是,鉴于上面的示例代码,我该怎么做?

是的,请看下面的论坛post:

http://forums.asp.net/t/1443955.aspx

您正在寻找的RegEx模式类似于以下内容:

 "(?]*)Jon Doe(?]*<)" 

基本上,你正在搜索并替换括号<>之外的任何内容。

JavaScript的:

 phrase = phrase.replace(/"(?]*)Jon Doe(?]*<)" /i, "is not"); 

使用正则表达式解析HTML? 这不是一个好主意 。 我建议将HTML插入DOM,然后遍历节点。