简体   繁体   English

用于在字符串中查找标记的 C# 正则表达式模式

[英]C# Regex pattern for finding a tag in a string

For the below string,I want to select only the inner script tag containing the url http://cdn.walkme.com/users and replace the selected tag with an empty string so can somebody help me with the regex pattern对于下面的字符串,我只想选择包含 url http://cdn.walkme.com/users的内部脚本标签,并用空字符串替换所选标签,这样有人可以帮助我使用正则表达式模式

<script><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></script>

I have tried this < script(.+)http://cdn.walkme.com/users/.+?\\/script>我试过这个< script(.+)http://cdn.walkme.com/users/.+?\\/script>

I agree that it's not really possible to have comprehensive and generic regex to parse any (x)HTML which standard supports.我同意使用全面和通用的正则表达式来解析标准支持的任何 (x)HTML 是不可能的。 That's is true just by nature of these things.就这些事物的性质而言,这是真的。

But you're perfectly fine to do lots of smaller cool tasks using Regex.但是您完全可以使用 Regex 完成许多较小的很酷的任务。 Just like in your case, in order to strip particular script out of the page markup, you could just use the following regex to find an entry and then replace it with an empty string:就像您的情况一样,为了从页面标记中删除特定脚本,您可以使用以下正则表达式来查找条目,然后将其替换为空字符串:

\<script\>\<script type="text/javascript"\>\(function\(\) \{var walkme =.*\</script\>

It does very a simple thing - takes everything in between它做了一件非常简单的事情 - 介于两者之间

<script><script type="text/javascript">(function() {var walkme = 

(you can include more text to be more specific) and (您可以包含更多文本以更具体)和

</script>

Just ensure special symbols (like /, ( or )) are escaped properly.只需确保正确转义特殊符号(如 /、(或))。

Edited In order to select inner need to use what is called positive lookahead to find first closing tag right after opening one:编辑为了选择内部需要使用所谓的正向前瞻在打开一个后立即找到第一个结束标签:

<script type="text/javascript">\(function\(\) {var walkme =.*(?=</script>)

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM