FRIHOSTFORUMSFAQTOSBLOGSDIRECTORY
You are invited to Log in or Register a Frihost Account!

[JS] Regular expressions global match get first group

 


Stubru Freak
I have written a (javascript) regex to match stylesheets in a page (css stylesheets between <style> tags to be more precise)

This code will populate an array with index 1: the complete first match, and 2: the first group of the first match:

Code:
var stylesheets = asourceCode.match(/< *style *(?:type=['"]?text\/css['"]?)? *>[\r\n ]*(?:<!?--)?([^]+?)(?:(?:\/\/)?-->)?[\r\n ]*< *\/ *style *>/i);


While this code will populate an array with all matches (complete):

Code:
var stylesheets = asourceCode.match(/< *style *(?:type=['"]?text\/css['"]?)? *>[\r\n ]*(?:<!?--)?([^]+?)(?:(?:\/\/)?-->)?[\r\n ]*< *\/ *style *>/gi);


Now I want an array containing the first group for all matches (or maybe a multidimensional array containing both the complete match and the first group for all matches).

How can I achieve this?

Is the only possible way to do an additional regular expression on every element of the array?

This is the code for a firefox extension so it doesn't have to be cross-browser compatible.

Thanks,
Frederik Vanderstraeten
Stubru Freak
Is there noone who knows this?
Reply to topic    Frihost Forum Index -> Scripting -> Html, CSS and Javascript

FRIHOST HOME | FAQ | TOS | ABOUT US | CONTACT US | SITE MAP
© 2005-2007 Frihost, forums powered by phpBB.