regex - Performing group extraction without while loop condition in javascript? -


scenario

extracting urls multiple css url() functional notation. given following css value project bootstrap:

src: url('../fonts/glyphicons-halflings-regular.eot?#iefix') format('embedded-opentype'), url('../fonts/glyphicons-halflings-regular.woff2') format('woff2'), url('../fonts/glyphicons-halflings-regular.woff') format('woff'), url('../fonts/glyphicons-halflings-regular.ttf') format('truetype'), url('../fonts/glyphicons-halflings-regular.svg#glyphicons_halflingsregular') format('svg'); 

i need array of url strings ["../fonts/glyphicons-halflings-regular.eot?#iefix", "../fonts/glyphicons-halflings-regular.woff2", ...].

solution

right use while loop , regex matcher extract url strings parsed declaration via css parser:

var decvalue = "url(foo), url(bar)"; // no data uri var regexp = new regexp(/url\(([^\(|^\)|^\"|^\']+)\)/g),     matches = [],     match;  while (match = regexp.exec(decvalue), match !== null) {     if (match[1].indexof("data:") === -1) {         matches.push(match[1]);     } }  // should print ["foo", "bar"] console.log(matches); 

question

is there way without using while loop keeping group matching?

because javascript doesn't support lookbehinds, have creative when text behind position matters don't want capture it. regex accounts single , double quotation marks.

here's 1 method uses replace push array, doesn't modify original variable.

var css = "src: url('../fonts/glyphicons-halflings-regular.eot?#iefix') format('embedded-opentype'), url('../fonts/glyphicons-halflings-regular.woff2') format('woff2'), url(\"../fonts/glyphicons-halflings-regular.woff\") format('woff'), url(../fonts/glyphicons-halflings-regular.ttf) format('truetype'), url('../fonts/glyphicons-halflings-regular.svg#glyphicons_halflingsregular') format('svg');"    var garnerurls = [];    css.replace(/url\(('[^']*'|"[^"]*"|[^\)]*)\)/g,function(match,p1) {  if (p1.charat(0) == "'" || p1.charat(0) == "\"") {      garnerurls.push(p1.substr(1,p1.length-2))  } else {      garnerurls.push(p1)  }          });    console.log(garnerurls)    console.log(css)

url\(('[^']*'|"[^"]*"|[^\)]*)\) 

explanation:

 url                # literal url  \(                 # literal (  (                  # opens cg1      '              # literal '      [^']*          # negated character class (excludes characters within)      '              # literal '  |                  # alternation (cg1)      "              # literal "      [^"]*          # negated character class (excludes characters within)      "              # literal "  |                  # alternation (cg1)      [^\)]*         # negated character class (excludes characters within)  )                  # closes cg1  \)                 # literal ) 

any of answers here, along current method, involve loop. if javascript supported lookbehinds, simple match() method still loops on list while finds further matches. in end, of these solutions fine solution.


Comments

Popular posts from this blog

java - Spring Data JPA: Why findOne(id) executing delete query internally? -

python - Mongodb How to add addtional information when aggregating? -

java - Incorrect order of records in M-M relationship in hibernate -