[英]javascript regex single-line
I'm trying to replace all @import strings in one single-string, eg 我试图在一个单字符串中替换所有@import字符串,例如
var str = 'buttons .reprint{height:56px;background-position:-47px -1496px;}@import "../common/buttons.css";@import "../screen/screen-1.css";@import "../screen/screen-2.css";@import "../screen/screen-3.css";@import "../screen/screen-5.css";@import "../screen/screen-6.css";@import "../screen/screen-7.css";@import "../screen/screen-quick-messages.css";@import "../screen/reset-password.css";#xml-data{display:none;}body{font-family:"Helvetica Neue",HelveticaNeue,Helvetica-Neue,Helvetica,sans-serif;}ul{padding:0;}body,section{width:768px;}';
It works if I write: 如果我写:
console.log(str.match(/@import\ /g));
But how to specify, all @import declaration at all in single line, eg @import "../screen/screen-1.css";
但是如何在单行中指定所有@import声明,例如@import "../screen/screen-1.css";
console.log(str.match(/@import\ .+;/g));
doesn't work. 不起作用。
Correct me if I'm wrong, but it seems like an @import
always has this format: 如果我错了,请纠正我,但似乎@import
总是有这种格式:
@import (whitespace) (quote) (one or more non-quote characters) (quote) (semicolon) @import(空白)(引用)(一个或多个非引号字符)(引用)(分号)
If that's true, then let's build the regex accordingly: 如果这是真的,那么让我们相应地构建正则表达式:
/@import\s*"[^"]+";/
This should match each @import
statement separately. 这应该分别匹配每个@import
语句。
I know it's already answered, but I just want to offer a simpler version of the pattern above. 我知道它已经回答了,但我只是想提供一个更简单的上述模式版本。
I needed this regex for removing @import statements from css files I merged together. 我需要这个正则表达式从我合并在一起的css文件中删除@import语句。
It matches the pattern: 它匹配模式:
@import (anything NOT a semicolon) (semicolon)
Works great. 效果很好。 Here's an example of how I use it: 这是我如何使用它的一个例子:
// $mergedStyles set earlier in script...
// matches the pattern
// ["@import"]+[anything NOT a semicolon]+semicolon
// e.g.: "@import url("stuff");
$pattern = '/@import[^;]*;/';
$mergedStyles = preg_replace($pattern, '', $mergedStyles);
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.