简体   繁体   English

匹配不在span标签内的文本

[英]Match text not inside span tags

Using Javascript, I'm trying to wrap span tags around certain text on the page, but I don't want to wrap tags around text already inside a set of span tags. 使用Javascript,我试图在页面上的某些文本周围包装span标签,但我不想在一组span标签内部的文本周围包装标签。

Currently I'm using: 目前我正在使用:

html = $('#container').html();
var regex = /([\s| ]*)(apple)([\s| ]*)/g;
html = html.replace(regex, '$1<span class="highlight">$2</span>$3');

It works but if it's used on the same string twice or if the string appears in another string later, for example 'a bunch of apples' then later 'apples', I end up with this: 它可以工作,但如果它在同一个字符串上使用两次,或者如果字符串稍后出现在另一个字符串中,例如'一堆苹果',然后是'苹果',我最终得到这个:

<span class="highlight">a bunch of <span class="highlight">apples</span></span>

I don't want it to replace 'apples' the second time because it's already inside span tags. 我不希望它第二次替换'apples',因为它已经在span标签内。

It should match 'apples' here: 它应该符合'苹果'这里:

Red apples are my <span class="highlight">favourite fruit.</span>

But not here: 但不是这里:

<span class="highlight">Red apples are my favourite fruit.</span>

I've tried using this but it doesn't work: 我尝试过使用它,但它不起作用:

([\s|&nbsp;]*)(apples).*(?!</span)

Any help would be appreciated. 任何帮助,将不胜感激。 Thank you. 谢谢。

First off, you should know that parsing html with regex is generally considered to be a bad idea—a Dom parser is usually recommended. 首先,您应该知道使用正则表达式解析html通常被认为是一个坏主意 - 通常建议使用Dom解析器。 With this disclaimer, I will show you a simple regex solution. 有了这个免责声明,我将向您展示一个简单的正则表达式解决方案。

This problem is a classic case of the technique explained in this question to "regex-match a pattern, excluding..." 这个问题是这个问题中解释为“正则表达式匹配模式,排除......”的经典案例。

We can solve it with a beautifully-simple regex: 我们可以用一个非常简单的正则表达式解决它:

<span.*?<\/span>|(\bapples\b)

The left side of the alternation | 交替的左侧| matches complete <span... /span> tags. 匹配完整的<span... /span>标记。 We will ignore these matches. 我们将忽略这些匹配。 The right side matches and captures apples to Group 1, and we know they are the right ones because they were not matched by the expression on the left. 右侧匹配并将apples捕获到第1组,我们知道它们是正确的,因为它们与左侧的表达不匹配。

This program shows how to use the regex (see the results in the right pane of the online demo ). 该程序显示了如何使用正则表达式(请参阅在线演示的右侧窗格中的结果)。 Please note that in the demo I replaced with [span] instead of <span> so that the result would show in the browser (which interprets the html): 请注意,在演示中我用[span]而不是<span>替换,以便结果显示在浏览器中(解释html):

var subject = 'Red apples are my <span class="highlight">favourite apples.</span>';
var regex = /<span.*?<\/span>|(\bapples\b)/g;
replaced = subject.replace(regex, function(m, group1) {
    if (group1 == "" ) return m;
    else return "<span class=\"highlight\">" + group1 + "</span>";
});
document.write("<br>*** Replacements ***<br>");
document.write(replaced);

Reference 参考

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM