Vim 可变长度通配符搜索和替换?

发布于 2024-12-28 02:12:48 字数 561 浏览 3 评论 0原文

我正在尝试清理 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

吾家有女初长成 2025-01-04 02:12:48

这应该消除 HTML 中的所有样式属性:

:%s/ style=".*"//g

编辑:Sam Brinck 提出了一个很好的观点。我的代码仅基于您的示例。如果 style="..." 属性后面还有其他属性,那么这段代码会占用太多资源。更安全的替代方案可能是:

:%s/ style="[^"]*"//g

这意味着 - 删除 style=" 之后的所有字符,即 不是双引号 [^"] 直到下一个遇到双引号。谢谢萨姆!

This should eliminate all the style attributes in your HTML:

:%s/ style=".*"//g

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:

:%s/ style="[^"]*"//g

which means - remove all characters after style=" that is NOT a double quote [^"] until the next double quote is encountered. Thanks Sam!

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文