在使用jQuery替换时,是否可以忽略字符串中的HTML? [英] Can you ignore HTML in a string while doing a Replace with jQuery?
问题描述
可能重复:
替换字符串中的单词,但忽略HTML
调用Replace时是否可以忽略HTML元素?
Is it possible to ignore the HTML elements when calling Replace?
示例代码:
$myText.replace(new RegExp( $searchString, 'gi' ),
'<span class="highlight">'+ $searchString + '</span>');
$ myText
是一大串HTML例如:
$myText
is a large string of HTML e.g.:
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, " +
"<a href="#" title="Lorem">but</a> 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.</p>"
$ searchString
等于用户类型为文本输入框。
$searchString
is equal to whatever a user types into an text input box.
如果是,在给定上述示例代码的情况下我该怎么做?
If yes, how would I do it given the above sample code?
推荐答案
是的,请查看以下论坛帖子:
Yes, take a look at the following forum post:
http://forums.asp.net/t/1443955.aspx
RegEx您正在寻找的模式类似于以下内容:
The RegEx pattern you are looking for would be something similar to the following:
"(?<!<[^>]*)Jon Doe(?<![^>]*<)"
基本上,你正在搜索并替换括号以外的任何东西<>。
Basically, you're doing a search and replace on anything that lives outside brackets <>.
JavaScript:
JavaScript:
phrase = phrase.replace(/"(?<!<[^>]*)Jon Doe(?<![^>]*<)" /i, "is not");
这篇关于在使用jQuery替换时,是否可以忽略字符串中的HTML?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!