正则表达式从任何HTML标记(style =“”)中删除HTML属性?

时间:2011-09-23 12:39:25

标签: regex asp-classic

我正在寻找一个正在寻找HTML标签内属性的正则表达式模式。具体来说,我想找到......的所有实例。

style=""

...并将其从包含在其中的HTML标记中删除。显然,这将包括双引号中包含的任何内容。

我正在使用Classic ASP来做到这一点。我已经为不同的正则表达式模式设置了函数,该模式查找字符串中的所有HTML标记并将其删除。它很棒。但现在我只需要另一种模式来专门删除所有样式属性。

非常感谢任何帮助。

7 个答案:

答案 0 :(得分:26)

也许更简单的表达是

 style="[^\"]*"

所以双引号之间的所有内容除了双引号外。

答案 1 :(得分:18)

我认为这可能会这样做:

/style="[a-zA-Z0-9:;\.\s\(\)\-\,]*"/gi

如果您只想更换某些部件,也可以将它们放入捕获组中

/(style=")([a-zA-Z0-9:;\.\s\(\)\-\,]*)(")/gi

工作示例: http://regexr.com?2up30

答案 2 :(得分:1)

我尝试了Jason Gennaro's正则表达式并略微修改了它

/style="[a-zA-Z0-9:;&\."\s\(\)\-\,]*|\\/ig

此正则表达式捕获字符串中&quot的一些特定情况,例如

 <div class="frame" style="font-family: Monaco, Consolas, &quot;Courier New&quot;, monospace; font-size: 12px; background-color: rgb(245, 245, 245);">some text</div>

答案 3 :(得分:0)

这适用于perl。也许您需要更改正则表达式以匹配ASP规则,但它应该适用于任何标记。

$file=~ s/(<\s*[a-z][a-z0-9]*.*\s)(style\s*=\s*".*?")([^<>]*>)/$1 $3/sig;

其中line是html文件。

这也是.net C#

      string resultString = null;
      string subjectString = "<html style=\"something\"> ";

      resultString = Regex.Replace(subjectString, @"(<\s*[a-z][a-z0-9]*.*\s)(style\s*=\s*"".*?"")([^<>]*>)", "$1 $3", RegexOptions.Singleline | RegexOptions.IgnoreCase);

结果:<html >

答案 4 :(得分:0)

这个表达对我有用:

style=".+"/ig

答案 5 :(得分:0)

尝试一下,它将完全替换样式属性,并且完全有价值

const regex = /style="(.*?)"/gm;
const str = `<div class="frame" style="font-family: Monaco, Consolas, &quot;Courier New&quot;, monospace; font-size: 12px; background-color: rgb(245, 245, 245);">some text</div>`;
const subst = ``;

// The substituted value will be contained in the result variable
const result = str.replace(regex, subst);

console.log('Substitution result: ', result);

答案 6 :(得分:0)

以下表达式应删除style属性(包括属性本身)中的所有 ;重要的是,这包括该属性使用双引号还是单引号:

/style=("|')(?:[^\1\\]|\\.)+?\1/gi

这会拆分捕获组,以便它们可以在单双引号上匹配,然后捕获之间的所有内容,包括URL编码的字符和换行符,同时保留其他属性(如类)或名称)。

在这里测试:https://regexr.com/4rovf

相关问题