如何在 HTML 文本中搜索特定文本并用颜色突出显示搜索字符串 [英] How to search a particular text within an HTML Text and highlight the search string with a color

查看:60
本文介绍了如何在 HTML 文本中搜索特定文本并用颜色突出显示搜索字符串的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我们需要搜索 HTML 文本并使用特定颜色突出显示文本.

We have requirement to search through a HTML text and highlight the text with a particular color.

例如:html字符串是

Ex: The html string is

<p class="Pa0" style="margin: 0in 0in 0pt;"><span class="A1"><span style="font-size: 10pt;">Giving this flyer to your class customers</span></span></p>

在上面的文本中,我正在搜索类"这个词.它应该突出显示文本将此传单给您的班级客户"中的文本 class但是,当它试图突出显示 css 元素class"时,结果原始 html 文本已损坏且无法正确显示.

In the above text I am searching for the word "Class". It should highlight the text class within the text "Giving this flyer to your class customers" But whereas it is trying to highlight the css element "class" and as a result the original html text is corrupted and not displayed properly.

任何人都可以帮助我解决有关如何在 HTML 文本中搜索内容并突出显示搜索而不更改原始 HTML 的问题.

Can anyone help me in resolving this issue on how to search a content within an HTML text and highlight the search without changing the original HTML.

推荐答案

(class)(?=(?:[^><]*<[^<>]*>)*[^><]*$)

试试这个.替换为 $1.看演示.

Try this.Replace by <span style='color: red;'>$1<span>.See demo.

http://regex101.com/r/bB8jY7/3

这篇关于如何在 HTML 文本中搜索特定文本并用颜色突出显示搜索字符串的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆