Warning: file_get_contents(/data/phpspider/zhask/data//catemap/6/xamarin/3.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Javascript 用正则表达式剥离css?_Javascript_Php_Regex - Fatal编程技术网

Javascript 用正则表达式剥离css?

Javascript 用正则表达式剥离css?,javascript,php,regex,Javascript,Php,Regex,Regex对我来说相当混乱,所以让我在这里提供一个例子,希望有人能帮助我 我正在构建一个图标数组,css如下所示 .icon-download:before { content: "\f01a"; } .icon-upload:before { content: "\f01b"; } .icon-inbox:before { content: "\f01c"; } .icon-play-circle:before { content: "\f01d"; } 显然还有更多的图标,

Regex对我来说相当混乱,所以让我在这里提供一个例子,希望有人能帮助我

我正在构建一个图标数组,css如下所示

.icon-download:before {
  content: "\f01a";
}
.icon-upload:before {
  content: "\f01b";
}
.icon-inbox:before {
  content: "\f01c";
}
.icon-play-circle:before {
  content: "\f01d";
}
显然还有更多的图标,但我需要获取图标名,而不是遍历每个图标并将其擦除,我想使用正则表达式来擦除
。图标-
保留图标名,然后从
:从
之前到
}

我读过关于regex的书,但是有很多不同的答案和问题,替换特殊字符、数字值1-10和字母a-z的一般方法是什么

因此,将
.icon-
替换为空白将很容易,而且
:在{content:\
之前,随机数会变得棘手

所以我的问题是,对于我要实现的目标,什么是最好的语法

".icon-download:before { content: \"\f01a\";}".replace(/\.icon\-(.*?):before {.*?}/, "$1")
我不写Javascript代码,但上面的代码似乎有效,可能会给您一些提示。请注意,您必须转义原始字符串中的双引号。您要寻找的具体技术是

我从中得到了灵感

我不写Javascript代码,但上面的代码似乎有效,可能会给您一些提示。请注意,您必须转义原始字符串中的双引号。您要寻找的具体技术是


我从中得到灵感。

这取决于你将在哪里使用正则表达式。什么语言,什么程序等。据我所知,你想删除
。图标-
,保留图标名称,然后删除其余的

以下是一个(最低限度的)正则表达式,可在记事本++中使用,以找到并替换所需的方式:

在\{\r\n.+\r\n\}

替换为:

输出:

download
upload
inbox
play-circle

这取决于您将在何处使用正则表达式。什么语言、什么程序等。据我所知,您希望删除
。图标-
,保留图标名称,然后删除其余的

以下是一个(最低限度的)正则表达式,可在记事本++中使用,以找到并替换所需的方式:

在\{\r\n.+\r\n\}

替换为:

输出:

download
upload
inbox
play-circle

我用JavaScript很快想出了最简单的表单

    // add more matches here
var pattern = /(\.icon\-(download|inbox|play\-circle))/g;

var m;

var text = '.icon-download:before {'+
                'content: "\f01a";'+
        '}'+
        '.icon-upload:before {'+
        '  content: "\f01b";'+
        '}'+
        '.icon-inbox:before {'+
        '  content: "\f01c";'+
        '}'+
        '.icon-play-circle:before {'+
        '  content: "\f01d";'+
        '}';

var reg = new RegExp(pattern);

while((m = reg.exec(text)) != null){
   alert(m[0]); 
}

我用JavaScript很快想出了最简单的表单

    // add more matches here
var pattern = /(\.icon\-(download|inbox|play\-circle))/g;

var m;

var text = '.icon-download:before {'+
                'content: "\f01a";'+
        '}'+
        '.icon-upload:before {'+
        '  content: "\f01b";'+
        '}'+
        '.icon-inbox:before {'+
        '  content: "\f01c";'+
        '}'+
        '.icon-play-circle:before {'+
        '  content: "\f01d";'+
        '}';

var reg = new RegExp(pattern);

while((m = reg.exec(text)) != null){
   alert(m[0]); 
}

你说你想建立一个数组。因此不需要替换或删除。只需提取图标名称

在php中,您可以这样做:

$css = readfile( ... ); // get css as a string
preg_match_all ('/icon-([-a-z]+):/', $css, $matches);
$result = $matches[1];
您肯定想阅读php手册中的regex文档: .
它是完整且易于理解的。

你说你想构建一个数组。因此不需要替换或删除。只需提取图标名称即可

/.icon-([a-z-]*):before {[^\}]*}/gm
在php中,您可以这样做:

$css = readfile( ... ); // get css as a string
preg_match_all ('/icon-([-a-z]+):/', $css, $matches);
$result = $matches[1];
您肯定想阅读php手册中的regex文档: . 它是完整的,易于遵循

/.icon-([a-z-]*):before {[^\}]*}/gm
使用此模式,您可以迭代CSS文件中的所有图标名称。通过修改
[a-z-]
部分,您可以定义这些名称可以包含的字符。在我的示例中,我使用了
a
z
和破折号(
-

Java代码示例:

public static ArrayList<String> parseIconNames(String css) {
    ArrayList<String> names = new ArrayList<String>();

    Pattern pattern = Pattern.compile(".icon-([a-z-]*):before {[^\\}]*}", Pattern.MULTILINE);
    Matcher matcher = pattern.matcher(css);

    while(matcher.find()) {
        names.add(matcher.group(1));
    }

    return names;
}
publicstaticarraylistparseiconnames(字符串css){
ArrayList name=新的ArrayList();
Pattern=Pattern.compile(“.icon-([a-z-]*):在{[^\\}]*}”之前,Pattern.MULTILINE);
Matcher-Matcher=pattern.Matcher(css);
while(matcher.find()){
name.add(matcher.group(1));
}
返回姓名;
}
使用此模式,您可以迭代CSS文件中的所有图标名称。通过修改
[a-z-]
部分,您可以定义这些名称可以包含的字符。在我的示例中,我使用了
a
z
和破折号(
-

Java代码示例:

public static ArrayList<String> parseIconNames(String css) {
    ArrayList<String> names = new ArrayList<String>();

    Pattern pattern = Pattern.compile(".icon-([a-z-]*):before {[^\\}]*}", Pattern.MULTILINE);
    Matcher matcher = pattern.matcher(css);

    while(matcher.find()) {
        names.add(matcher.group(1));
    }

    return names;
}
publicstaticarraylistparseiconnames(字符串css){
ArrayList name=新的ArrayList();
Pattern=Pattern.compile(“.icon-([a-z-]*):在{[^\\}]*}”之前,Pattern.MULTILINE);
Matcher-Matcher=pattern.Matcher(css);
while(matcher.find()){
name.add(matcher.group(1));
}
返回姓名;
}


您想用哪种语言实现这一点?哦,对不起,这只是使用正则表达式查找和替换。如果说javascript或php会更容易,那么我愿意接受。请确保包含一个正确格式的预期输出示例,以便人们理解您试图实现的结果。我建议您添加lan用语言标记你的问题,这样它就可以得到更多的关注:)对不起,但我从来没有在另一种语言中使用过正则表达式,我总是使用它只是为了查找和替换..你想用什么语言来实现这一点?哦,对不起,这只是使用正则表达式查找和替换。如果说javascript或php会更容易,那么无论如何,我对t帽子。请确保包含一个正确格式的预期输出示例,以便人们理解您试图实现的结果。我建议您在问题中添加语言标记,以便引起更多的注意:)抱歉,我从未在其他语言中使用过regex,我一直使用它只是为了查找和替换..您是所以需要转义反斜杠。@LorenzMeyer-Where?代码似乎在下面工作。在输入字符串中:“.icon-download:before{content:\“\\f01a\”;}”。您还需要转义反斜杠。@LorenzMeyer-Where?代码似乎在下面工作。在输入字符串中:“.icon-download:before{content:\“\\f01a\”;}“.Cool stuff从来没有想过提取单词这是一个很好的将来使用的资源…Cool stuff从来没有想过提取单词这是一个很好的将来使用的资源…+1我认为你是唯一一个理解OP真正需要什么的人。+1我认为你是唯一一个理解OP真正需要什么的人。