替换字符串中的单词,但忽略HTML [英] Replace words in a string, but ignore HTML
问题描述
我正在尝试编写一个高亮插件,并希望保留HTML格式。可以忽略<之间的所有字符吗?和使用javascript进行替换时在字符串中?
I'm trying to write a highlight plugin, and would like to preserve HTML formatting. Is it possible to ignore all the characters between < and > in a string when doing a replace using javascript?
使用以下示例:
var string = "Lorem ipsum dolor span sit amet, consectetuer <span class='dolor'>dolor</span> adipiscing elit.";
我希望能够实现以下目标(将'dolor'替换为'FOO'):
I would like to be able to achieve the following (replace 'dolor' with 'FOO'):
var string = "Lorem ipsum FOO span sit amet, consectetuer <span class='dolor'>FOO</span> adipiscing elit.";
或者甚至是这个(用'BAR'代替'span'):
Or perhaps even this (replace 'span' with 'BAR'):
var string = "Lorem ipsum dolor BAR sit amet, consectetuer <span class='dolor'>dolor</span> adipiscing elit.";
我非常接近tambler给出的回答:你可以在用jQuery替换时忽略字符串中的HTML吗?但是,出于某种原因,我无法得到已接受的工作答案。
I came very close to finding an answer given by tambler here: Can you ignore HTML in a string while doing a Replace with jQuery? but, for some reason, I just can't get the accepted answer to work.
我对正则表达式完全不熟悉,所以任何帮助都将不胜感激。
I'm completely new to regex, so any help would be gratefully appreciated.
推荐答案
使用浏览器的内置解析器通过 innerHTML
解析HTML通过DOM遍历是实现这一目标的明智方法。以下是基于此答案的答案:
Parsing the HTML using the browser's built-in parser via innerHTML
followed by DOM traversal is the sensible way to do this. Here's an answer loosely based on this answer:
现场演示: http://jsfiddle.net/FwGuq/1/
代码:
// Reusable generic function
function traverseElement(el, regex, textReplacerFunc) {
// script and style elements are left alone
if (!/^(script|style)$/.test(el.tagName)) {
var child = el.lastChild;
while (child) {
if (child.nodeType == 1) {
traverseElement(child, regex, textReplacerFunc);
} else if (child.nodeType == 3) {
textReplacerFunc(child, regex);
}
child = child.previousSibling;
}
}
}
// This function does the replacing for every matched piece of text
// and can be customized to do what you like
function textReplacerFunc(textNode, regex, text) {
textNode.data = textNode.data.replace(regex, "FOO");
}
// The main function
function replaceWords(html, words) {
var container = document.createElement("div");
container.innerHTML = html;
// Replace the words one at a time to ensure each one gets matched
for (var i = 0, len = words.length; i < len; ++i) {
traverseElement(container, new RegExp(words[i], "g"), textReplacerFunc);
}
return container.innerHTML;
}
var html = "Lorem ipsum dolor span sit amet, consectetuer <span class='dolor'>dolor</span> adipiscing elit.";
alert( replaceWords(html, ["dolor"]) );
这篇关于替换字符串中的单词,但忽略HTML的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!