将粗体/强标签包裹在内容片段中第一次出现的关键字周围?
我正在寻找一种最简单的方法,当该短语未出现在标题标签中或作为 html 属性值时,将粗体标签包裹在预定义关键字短语的第一次出现周围。找到第一个匹配项后,退出例程。
例如,如果关键字是“blue widgets”,内容是:
blue widgets and accessories for blue widgets can be found here
那么在例程过滤内容之后,它将返回:
<b>blue widgets</b> and accessories for blue widgets can be found here
但是,如果单词“blue widgets”第一次出现在属性或标题标记中,它会跳过那些并转到下一个。例如,
<img src="foo.png" title="A site about blue widgets" alt="blue-widget" />
<h2>This is a site about blue widgets</h2>
<p>We've got lots of blue widgets and blue widget accessories...
在上面的内容中,只有句子“We’ve got much of blue widgets and blue widget Accessories”中出现的关键字会被加粗。
有人能给我举个例子来说明如何做到这一点吗?
I'm looking for the simplest way to wrap bold tags around the first appearance of a predefined keyword phrase, when that phrase does not appear in a heading tag or as an html attribute value. After the first match is found, exits the routine.
For example, if the keyword is "blue widgets", and the content was:
blue widgets and accessories for blue widgets can be found here
Then after the routine filters the content, it would return:
<b>blue widgets</b> and accessories for blue widgets can be found here
However, if the first occurrence of the word "blue widgets" were in an attribute or a heading tag, it would skip over those and go to the next one. For example,
<img src="foo.png" title="A site about blue widgets" alt="blue-widget" />
<h2>This is a site about blue widgets</h2>
<p>We've got lots of blue widgets and blue widget accessories...
In the above content, only the appearance of the keyword in the sentence "We've got lots of blue widgets and blue widget accessories"... would be bolded.
Can someone give me an example of how this can be done?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
如果您仍在考虑使用正则表达式,请查看以下内容:
在 ideone.com 上运行它< /strong>
我什至不确定是否可以使用正则表达式来做到这一点,这就是为什么我不厌其烦地解决这个问题。尽管这个解决方案很糟糕,但它是我能做到的最简单的。为此,我必须忽略许多可能破坏它的因素,例如 CDATA 部分、SGML 注释、
元素以及属性值中的尖括号等。这只是有效 HTML 中的内容。
尽管这很有趣,但我希望它能说服您一劳永逸地忘记正则表达式并使用专用工具,正如其他响应者所建议的那样。
If you're still thinking about using a regex, check this out:
run it on ideone.com
I wasn't even sure it was possible to do this with a regex, which is why I went to the trouble of working it out. Hideous as this solution is, it's about as simple as I could make it. And to do that I had to ignore many factors that can break it--things like CDATA sections, SGML comments,
<script>
elements, and angle brackets in attribute values, to name a few. And that's just in valid HTML.Fun as this was, I hope it persuades you once and for all to forget about regexes and use a dedicated tool, as the other responders advised.