HTML 替换的正则表达式
您好,我正在尝试找到 RegEx,它可以帮助我替换 HTML 中的单词。如果我尝试替换的单词也在 HTML 标记中,则会出现问题。
示例:asd TEST asd dsa asd
我只需要获得第二个“测试”。
我正在寻找的正则表达式应该看起来像 >[^<]*TEST
,但是这个正则表达式也在单词 TEST 之前接受字符。是否可以只选择单词 TEST ?但也可以想象其他组合(我不认为“ TEST ”是一个好的解决方案,因为文本也可以包含其他字符)
Hi I am trying to find RegEx which helps me to replace words in HTML. Problem occurs if the word i am trying to replace is in HTML tag as well.
Example:<img class="TEST">asd TEST asd dsa asd </img>
and i need to get the second "TEST" only.
RegEx i am looking for should look like >[^<]*TEST
, but this regex takes chars before the word TEST as well. Is it possible to select only word TEST ? but imagine other combinations as well (i dont think " TEST " is a good solution as soon as text could contain another chars as well)
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
首先,正则表达式对于 html 解析来说不是一个好的选择。您可以使用很多增强的 html 解析器。
但是如果您坚持使用正则表达式,这里是正则表达式;
对于java,
有关为什么我们不能在Java中使用*或+与lookbehind正则表达式的更多信息,Java 中没有明显最大长度的正则表达式后视
First of all, regex is not good option for html parsing.. There are lots of enhanced html parsers that you can use..
But if you insist to use regex , here is the regex ;
for java,
for more information why we can not use * or + with lookbehind regex in Java , Regex look-behind without obvious maximum length in Java
首先,就像已经说过并将再次说过的那样,对 XML 使用正则表达式通常是一个坏主意。但对于非常简单的情况,它可以工作,特别是如果你可以接受次优结果。
因此,只需将测试放入一个组中,然后仅替换该组
免责声明之类的内容
:未经测试,可能有一些错误。但这个概念应该很清楚。
First of all, like has been said and will be said again, using regex for XML is usually a bad idea. But for really simple cases it can work, especially if you can live with sub-optimal results.
So, just put the test in a group and replace only the group
Something like
Disclaimer: Not tested, might have some off-by-ones. But the concept should be clear.
如果“TEST”位于另一个标签内(例如在 body 标签内,或者在 html 标签内)怎么样?
How about if "TEST" is inside another tag than , like say inside the body tag, or for that matter inside the html tag?