搜索并突出显示 html - 忽略和维护标签
我正在寻找一种通过剥离 html 标签来突出显示 html 块中的关键字的好方法。我可以使用正则表达式来搜索 html 标签内的关键字,但我还没有找到跨标签搜索的好方法。例如,如果关键词短语是“未绑定”,我希望能够将其
<p>I am not<strong>bound to please thee</strong> with my answers.</p>
包裹在突出显示标签中,而不破坏“strong”标签(并使 html 无效)并成为:
<p>I am <span class="highlight">not</span><strong><span class="highlight">bound</span> to please thee</strong> with my answers.</p>
主要问题是维护html 本身并用突出显示标签包装文本块。我需要保留原来的html。否则我会剥掉标签。
我现在能想到的最佳解决方案是复制 html 并在每个空格出现的地方放置计数器标记,然后剥离所有标签并搜索匹配的短语,然后回顾原始字符串和标记化字符串并找出从哪里开始构建精彩片段标签,然后开始向前行走,根据需要从比赛开始到结束开始和结束精彩片段跨度。这似乎有点矫枉过正。如果可能的话,我想要更优雅的东西。
该解决方案将用 C# 或 JavaScript 编写,具体取决于情况。
I am looking for a good way to highlight key words in a block of html with stripping the html tags. I can regex to search for key words within html tags, but I haven't found a great way to search across tags. For example, if the key word phrase is "not bound" I want to be able to make this
<p>I am not<strong>bound to please thee</strong> with my answers.</p>
become wrapped in highlight tags, without breaking the "strong" tag (and making the html invalid) and become:
<p>I am <span class="highlight">not</span><strong><span class="highlight">bound</span> to please thee</strong> with my answers.</p>
The main issue is maintaining the html as it is AND wrapping blocks of text with highlight tags. I need to maintain the original html. Otherwise I would strip the tags.
The best solution to this that I can think of right now would entail making a copy of the html and placing counter tokens where each space occurs, then stripping all tags and search for matching phrases, then looking back to the original and the tokenized strings and figuring out where to start building the highlight tags, then start walking forward, starting and ending highlight spans as needed from the beginning of the match until the end. This seems like overkill. I would like to something more elegant if possible.
The solution would be written in C# or perhaps javascript, depending.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
当我需要更改标记中的重复实例,但又想节省时间时,我使用 Notepad++ 中的“查找和替换”功能。这是一个类似的问题,可能会有所帮助
在 Notepad++ 和 Vim 中使用正则表达式进行批量替换。
希望这有帮助。
When I need to change a repeating instance in my markup, but want to save time, I use "Find and Replace" feature in Notepad++. Here's a similar question which may be helpful
Using regular expressions to do mass replace in Notepad++ and Vim.
Hope this helps.