c#使用regex从字符串中删除脚本标记

时间:2018-02-12 17:00:49

标签: regex

我有一个包含一些数据/脚本标签的字符串。我想根据网址http://cdn.walkme.com/users搜索字符串中的以下脚本标记,并从字符串中删除此特定脚本标记

<script type="text/javascript">(function() {var walkme = document.createElement('script'); walkme.type = 'text/javascript'; walkme.async = true; walkme.src='http://cdn.walkme.com/users/cb643dab0d6f4c7cbc9d436e7c06f719/walkme_cb643dab0d6f4c7cbc9d436e7c06f719.js'; var s = document.getElementsByTagName('script')[0]; s.parentNode.insertBefore(walkme, s); window._walkmeConfig = {smartLoad:true}; })();</script>

1 个答案:

答案 0 :(得分:0)

您必须使用正则表达式支持 atomic 组的语言。

刮刀系列

查找
<script(?:\s+(?>"[\S\s]*?"|'[\S\s]*?'|(?:(?!/>)[^>])?)+)?\s*>[\S\s]*?http://cdn\.walkme\.com/users[\S\s]*?</script\s*(?=>)>

替换&#34;&#34;

https://regex101.com/r/8ldMoN/1

格式化/评论

 <script                            # Open script tag
 (?:
      \s+ 
      (?>                           # REQUIRED Atomic group to get complete tag
           " [\S\s]*? "
        |  ' [\S\s]*? '
        |  (?:
                (?! /> )            # Not a self contained tag (why atomic is necessary)
                [^>] 
           )?
      )+
 )?
 \s* 
 > 
                                    # What we want to find
 [\S\s]*? http://cdn\.walkme\.com/users [\S\s]*?  

 </script \s*                       # Close script tag
 (?= > )
 >