mickro
mickro

Reputation: 891

Regexp minimal match on javascript minified file

I've a minified file. The main particularity is there is only one line.

this could be an extract:

bla();someothercode('resources/images/myPicture1.png'),someothercode('resources/images/myPicture2.png'),someothercode('resources/images/myPicture3.png');plop();

I tried to catch with /\/(.*\.png) and I obtain:

myPicture1.png'),someothercode('resources/images/myPicture2.png'),someothercode('resources/images/myPicture3.png

instead of many result as:

[myPicture1.png, myPicture2.png, myPicture3.png]

How do I match all the pngs separately?

Upvotes: 0

Views: 110

Answers (2)

Paul Roub
Paul Roub

Reputation: 36438

To catch all "words" that end in .png, you can say:

/\b[^\/]+\.png/g

This will find filenames whether or not there is a preceding /.

var st = "bla();someothercode('resources/images/myPicture1.png'),someothercode('resources/images/myPicture2.png'),someothercode('resources/images/myPicture3.png');plop();";

var matches = st.match(/\b[^\/]+\.png/g);

console.log(matches);

Upvotes: 1

Wiktor Stribiżew
Wiktor Stribiżew

Reputation: 626927

Use the following regex:

\/([^\/]*\.png)

Here, \/ matches / and then ([^\/]*\.png) matches and captures the name of the PNG (as [^\/]* matches 0 or more characters other than /, the matches returned are as expected, and the expected values are inside Group 1).

See code:

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/>");
}

Upvotes: 3

Related Questions