如何在 HTML 文本中搜索特定文本并用颜色突出显示搜索字符串

编程入门 行业动态 更新时间:2024-10-27 05:28:23
本文介绍了如何在 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.

regex101/r/bB8jY7/3

更多推荐

如何在 HTML 文本中搜索特定文本并用颜色突出显示搜索字符串

本文发布于:2023-10-19 19:27:13,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1508534.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:字符串   文本   颜色   如何在   文本中

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!