drfm55597 2017-06-24 01:10
浏览 5
已采纳

标签的正则表达式有效,但不具有特定的ID?

The following regex:

(?!<script[^>]*>)[(.*?)](?![^<]*<\/script>)

Targets every [TEXT] and [INPUT] there is in the input string, except any [] within a script tag.

I would now like to change this, to have the exception to be on a specific script with id="special" instead.

So <script id="special">[INPUT]</script> should not be targeted while another script tag without the id special, like <script>[INPUT]</script> should together with the rest of the string.

I tried adding id="special" to the above regex before [^>]*>, but doesn't work.

  • 写回答

2条回答 默认 最新

  • dsa45132 2017-06-24 12:40
    关注

    You can skip everything inside a script element with that id by using the PCRE verbs skip and fail.

    <script id="special">.*?<\/script>(*SKIP)(*FAIL)|\[[^\]]+?\]
    

    Demo: https://regex101.com/r/PSMV15/5/

    You can read more about this here, http://www.rexegg.com/backtracking-control-verbs.html#skipfail.

    If a string is HTML a parser should be used because there can be all sorts of variations in the elements and attributes.

    For example:

    <script  id="special">
    <script src="page" id="special">
    <script src="page" id="special" class="why?">
    <script id='special'>
    <script id=special>
    <script id=special src=page>
    

    without even entering the layered elements issue. Here's one thread on why regexs and HTML shouldn't go together. RegEx match open tags except XHTML self-contained tags

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(1条)

报告相同问题?

悬赏问题

  • ¥50 comfyui下连接animatediff节点生成视频质量非常差的原因
  • ¥20 有关区间dp的问题求解
  • ¥15 多电路系统共用电源的串扰问题
  • ¥15 slam rangenet++配置
  • ¥15 有没有研究水声通信方面的帮我改俩matlab代码
  • ¥15 对于相关问题的求解与代码
  • ¥15 ubuntu子系统密码忘记
  • ¥15 信号傅里叶变换在matlab上遇到的小问题请求帮助
  • ¥15 保护模式-系统加载-段寄存器
  • ¥15 电脑桌面设定一个区域禁止鼠标操作