在标签中包装单词,保留标记

时间:2012-06-03 16:31:31

标签: javascript html parsing

例如,我有一个带有标记的字符串(来自html节点):

h e llo,thi s i s d og

"h<em>e<strong>llo, thi</strong>s i</em><strong>s d</strong>og"

在其中找到一些单词的最正确的方法是什么(让我们说“你好”和“狗”),将它们包裹在一个范围内(突出显示)并保存所有标记?

所需的输出是这样的(注意正确关闭的标签)

<span class="highlight">h<em>e<strong>llo</strong></em></span><strong>,</strong> <em><strong>thi</strong>s<em> i</em><strong>s <span class="highlight"><strong>d</strong>og</span>

看起来应该如此:

h e llo thi si SD OG

1 个答案:

答案 0 :(得分:2)

你走了:

//Actual string
var string = "h<em>e<strong>llo, thi</strong>s i</em><strong>s d</strong>og";

//RegExp to cleanup html markup
var tags_regexp = /<\/?[^>]+>/gi;

//Cleaned string from markup
var pure_string = string.replace(tags_regexp,"");

//potential words (with original markup)
var potential_words = string.split(" ");

//potential words (withOUT original markup)
var potential_pure_words = pure_string.split(" ");

//We're goin' into loop here to wrap some tags around desired words
for (var i in potential_words) {

    //Check words here
    if(potential_pure_words[i] == "hello," || potential_pure_words[i] == "dog")

    //Wrapping...
    potential_words[i] = "<span class=\"highlight\">" + potential_words[i] + "</span>";
}

//Make it string again
var result = potential_words.join(" ");

//Happy endings :D
console.log(result);