有效的正则表达式在 CSS 属性值后强制使用单个空格
由于我经常在 Dreamweaver 中进行基本的 CSS 工作...DW 代码补全倾向于直接在冒号后面添加 CSS 属性值,不加空格...{attr:value}。由于挑剔的浏览器出现渲染问题,这导致了一些真正的调试难题,我经常发现自己必须在冒号后面手动添加空格。 [^DRY]
我目前正在构建一个简单的正则表达式片段,我可以运行它来正确格式化CSS...
我目前所拥有的似乎可以工作,但我认为它有点粗糙...我基本上只是在尝试看看是否有任何建议...这是我得到的...
(.*?(?={).*?:)([^\s][\w!#$%&'*+/=?^_`{|}~-]*.*?;})
并将该行替换为...
\s$2
► 澄清 ► 下面我对上面的解决方案进行了一些改进... 不过,我仍然认为它需要改进。 ..
(?:(?={*))(?:\s*?)([\s\r\n]*?\b[\w!#$%&'*+/=?^_`|~-]*:(?!.*{))(?!\s)(.*?;)
并替换为...
$1 $2
► Perl 版本►
Perl 使这个工作变得非常轻松......
perl -pi -e 's/^(.*?:)([^\s].*?;).*$/$1 $2/ig'
As I often work in Dreamweaver for basic CSS work ... DW code completion tends to add CSS attribute values directly after the colon without spacing ... {attr:value}. As this has caused some real debugging headaches due to rendering issues with picky browsers, I often find myself having to manually add the space after the colon. [^D.R.Y.]
I'm currently working on building a simple regex snippet that I can run to format the CSS properly ...
What I currently have seems to work but I think it's a bit crude ... I'm basically just trying to see if there are any suggestions ... Here's what I've got ...
(.*?(?={).*?:)([^\s][\w!#$%&'*+/=?^_`{|}~-]*.*?;})
And Replacing the line with ...
\s$2
► Clarification ► Below I've Somewhat Improved the Solution Above ... I Still Think It Needs Refinement Though ...
(?:(?={*))(?:\s*?)([\s\r\n]*?\b[\w!#$%&'*+/=?^_`|~-]*:(?!.*{))(?!\s)(.*?;)
And Replacing With ...
$1 $2
► Perl Version ►
Perl makes very light work of this ...
perl -pi -e 's/^(.*?:)([^\s].*?;).*$/$1 $2/ig'
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
如果您在 C# 程序中运行正则表达式片段,我可以为您提供解决方案:
If you run your regex snippet in a C# program, I have the solution for you:
如果我理解正确的话,这应该可以工作(JavaScript)
这给出了
'{attr: value}'
作为输出。If I understand you correctly, this should work (JavaScript)
This gives
'{attr: value}'
as the output.