我一直在尝试创建这个正则表达式,但它让我发疯。我基本上需要的是一个正则表达式,它可以检测所有<body>不是通过我的服务器端代码编写的标签。我的计划是将所有这些<body>标签替换为<body><%=CallToFunction()%>.
(这是搜索和替换的一部分UltraEdit。)
例如:
<body> //should be found
<body class="normal"> //should be found
<body class="<% Response.Write("normal") %>" //should be found
<html><body class="normal"> //should be found
Response.Write("<body class=""normal"">") //should not be found (a)
Response.Write(" <body>") //should not be found (b)
Response.Write("<html><body><h1>...") //should not be found (c)
message = "<html><body>...</body></html>" //should not be found (d)
Response.Write(message)
Response.Write("<html>
<head></head>
<body class=""normal"">
<h1>...</h1>") //should not be found (e)
我目前拥有的正则表达式是:([^"]<body.*[^>]*>). 但问题是它仍然会找到在和<body>之间有空格的标签(参见示例)。它仍然会找到.<body>"(a)(c)
因为(e)我真的一无所知。想知道这是否甚至可以检测到。
谁能帮我?
谢谢!
编辑
我现在有^(?!Response|")(<body.*[^>]*>)哪个效果很好。<body>但是当标签在文档中缩进时它不起作用。所以我需要一些东西,比如在 or 之外的任何东西(或<body什么都没有)前面。Response"
回答
我最终使用的正则表达式基于迈克尔艾伦的回答,并且是:
^(?!Response|")([\t ]*)(<body.*[^>]*>)
它没有解决(e),但我想我会为这些情况做一些手动工作。