我试图将一些正则表达式串在一起以查找 css 文件中使用的所有字体。到目前为止我的情况如下。
var regExp = /font-family:(\w+);/;
var matches = regExp.exec("font-family:arial; font-family:berch;");
//matches[1] contains the value between the parentheses
console.log(matches);
这仅匹配“arial”。我怎样才能让它继续搜索以找到“berch”?
最佳答案
向您的正则表达式添加一个 g
修饰符。
var regExp = /font-family:(\w+);/g;
g
(全局)修饰符表示在第一次匹配后不要停止并尽可能匹配模式。
也不要使用exec
。使用string.match
。
"font-family:arial; font-family:berch;".match(/font-family:(\w+);/g);
关于javascript - 使用正则表达式在 css 文件中查找字体,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/23056432/