javascript - javascript 缩小文件上的正则表达式最小匹配

标签 javascript regex

我有一个缩小的文件。主要特点是只有一行。

这可能是摘录:

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/

相关文章:

javascript - 动态改变对象的大小而不影响其他元素

javascript - 为什么缩进的文本仍然显示在 IE7 中?

javascript - 使用jquery删除不同表中相同位置的行

java - 使用正则表达式将缩写重新组合在一起

c# - 不允许在正则表达式中使用空格

javascript - 如何在客户端解码 JWT token 负载?

javascript - React-native UI 挑战

regex - 什么(?: do in a regular expression

html - 清理序列化 DOM 的安全正则表达式?

javascript - 了解使用 JavaScript 进行电子邮件验证