Vim 可变长度通配符搜索和替换?
我正在尝试清理 Frontpage 生成的 html 文件,并且需要删除大量标签属性,例如:
style="font-size: 10.0pt; font-family: Trebuchet MS; color: blue"
style="color: blue; text-decoration: underline; text-underline: single"
style="color: blue; text-decoration: underline; text-underline: single"
style="font-family: Trebuchet MS"
style="font-size:10.0pt;"
style="color: navy"
我可以使用简单的 .命令:
:%s/ style="........"//g
但是有没有办法使 .该替代命令中的可变长度,以便一个命令将删除整个文档中的每个样式属性?
PS - 我搜索了首页清理程序并找到了一些,但不清楚它们的可靠性如何,所以自己编写脚本。不过,请在这里接受建议。
I'm trying to clean a Frontpage-generated html file, and there are a ton of tag attributes I need to delete, like:
style="font-size: 10.0pt; font-family: Trebuchet MS; color: blue"
style="color: blue; text-decoration: underline; text-underline: single"
style="color: blue; text-decoration: underline; text-underline: single"
style="font-family: Trebuchet MS"
style="font-size:10.0pt;"
style="color: navy"
I can delete a set number of wildcards with a simple . command:
:%s/ style="........"//g
But is there a way to make the . variable length in that substitute command, so that one command will delete every style attribute in the whole document?
PS - I've searched for frontpage cleaners and found a few, but not clear how reliable they are, so scripting it myself instead. Open to suggestions here though.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
这应该消除 HTML 中的所有样式属性:
编辑:Sam Brinck 提出了一个很好的观点。我的代码仅基于您的示例。如果
style="..."
属性后面还有其他属性,那么这段代码会占用太多资源。更安全的替代方案可能是:这意味着 - 删除
style="
之后的所有字符,即 不是双引号[^"]
直到下一个遇到双引号。谢谢萨姆!This should eliminate all the style attributes in your HTML:
Edit: Sam Brinck brings up a good point. My code was based on your example alone. This code would gobble up too much, say if there were other attributes following the
style="..."
attribute. A safer alternative may be:which means - remove all characters after
style="
that is NOT a double quote[^"]
until the next double quote is encountered. Thanks Sam!