JavaScript - Regex - Grab only matched pattern if there are multiple matches in a single line -
i'm trying read contents of css file , looking links external files. figured faster write quick regular expression me, proving frustrating.
say have:
@font-face{font-family:'fontawesome';src:url('../fonts/fontawesome-webfont.eot?v=4.6.3');src:url('../fonts/fontawesome-webfont.eot?#iefix&v=4.6.3') format('embedded-opentype'),url('../fonts/fontawesome-webfont.woff2?v=4.6.3') format('woff2'),url('../fonts/fontawesome-webfont.woff?v=4.6.3') format('woff'),url('../fonts/fontawesome-webfont.ttf?v=4.6.3') that line goes on more, lets take now.
as can see, there both src:url(...) , url(...) figured url(..) occurrences.
here's tried: take 1:
var contents.match = ... // contents of .min.css file. var matches = contents.match(/url\s*(.*);/g); take 2:
var contents.match = ... // contents of .min.css file. var matches = contents.match(/url\s*(.*);/); //no g @ end. take 3:
var contents.match = ... // contents of .min.css file. var matches = contents.match(/url\s*\((.*)\);/g); take 4:
var contents.match = ... // contents of .min.css file. var matches = contents.match(/url\s*\((.*)\);/); // no g @ end. all want array of matches, either string between () or whole match, i.e. url(...); instead massive pipe out of entire file.
what want:
console.log(matches); /// prints ['fonts/font_1.css', 'fonts/font_2.css', 'fonts/font_3.css'] of
console.log(matches); /// prints ["url(fonts/font_1.css')", "url('fonts/font_2.css')", "url('fonts/font_3.css')"] where missing something?
the .* problem because it's going match closing parens too.
try this: /url\s*\([^)]*\)/g
Comments
Post a Comment