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

The*_*Guy 10 javascript jquery replace

可能重复:
替换字符串中的单词,但忽略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, " +
    "<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>"
Run Code Online (Sandbox Code Playgroud)

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

如果是,在给出上述示例代码的情况下,我该如何做?

tam*_*ler 6

是的,请看下面的论坛帖子:

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

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

"(?<!<[^>]*)Jon Doe(?<![^>]*<)"
Run Code Online (Sandbox Code Playgroud)

基本上,你正在搜索和替换生活在括号<>之外的任何东西.

JavaScript的:

phrase = phrase.replace(/"(?<!<[^>]*)Jon Doe(?<![^>]*<)" /i, "is not");
Run Code Online (Sandbox Code Playgroud)