我有一个缩小的文件。主要特点是只有一行。
这可能是摘录:
bla();someothercode('resources/images/myPicture1.png'),someothercode('resources/images/myPicture2.png'),someothercode('resources/images/myPicture3.png');plop();
我尝试用 /\/(.*\.png)
捕获并获得:
myPicture1.png'),someothercode('resources/images/myPicture2.png'),someothercode('resources/images/myPicture3.png
而不是许多结果:
[myPicture1.png, myPicture2.png, myPicture3.png]
如何分别匹配所有 png?
最佳答案
使用以下正则表达式:
\/([^\/]*\.png)
这里,\/
匹配 /
,然后 ([^\/]*\.png)
匹配并捕获PNG(如 [^\/]*
匹配除 /
之外的 0 个或多个字符,返回的匹配项符合预期,并且预期值位于第 1 组内)。
查看代码:
var re = /\/([^\/]*\.png)/g;
var str = 'bla();someothercode(\'resources/images/myPicture1.png\'),someothercode(\'resources/images/myPicture2.png\'),someothercode(\'resources/images/myPicture3.png\');plop();';
var m;
while ((m = re.exec(str)) !== null) {
document.write(m[1] + "<br/>");
}
关于javascript - javascript 缩小文件上的正则表达式最小匹配,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/32074771/