const regex = /@font-face\s* # Match @font-face and some spaces
( # Start group 1
\{ # Match {
(?: # A non-capturing group
[^{}]+ # Match anything except {} one or more times
| # Or
(?1) # Recurse/rerun the expression of group 1
)* # Repeat 0 or more times
\} # Match }
) # End group 1
(*SKIP)(*FAIL) # Skip it
| # Or
url\s*\( # Match url, optionally some whitespaces and then (
\s* # Match optionally some whitespaces
("|'|) # It seems that the quotes are optional according to http://www.w3.org/TR/CSS2/syndata.html#uri
(?!["']?(?:https?://|ftp://)) # Put your negative-rules here (do not match url's with http, https or ftp)
(?:[^\\]|\\.)*? # Match anything except a backslash or backslash and a character zero or more times ungreedy
\2 # Match what was matched in group 2
\s* # Match optionally some whitespaces
\) # Match )/gs;
// Alternative syntax using RegExp constructor
// const regex = new RegExp('@font-face\\s* # Match @font-face and some spaces
( # Start group 1
\\{ # Match {
(?: # A non-capturing group
[^{}]+ # Match anything except {} one or more times
| # Or
(?1) # Recurse\/rerun the expression of group 1
)* # Repeat 0 or more times
\\} # Match }
) # End group 1
(*SKIP)(*FAIL) # Skip it
| # Or
url\\s*\\( # Match url, optionally some whitespaces and then (
\\s* # Match optionally some whitespaces
("|\'|) # It seems that the quotes are optional according to http:\/\/www.w3.org\/TR\/CSS2\/syndata.html#uri
(?!["\']?(?:https?:\/\/|ftp:\/\/)) # Put your negative-rules here (do not match url\'s with http, https or ftp)
(?:[^\\\\]|\\\\.)*? # Match anything except a backslash or backslash and a character zero or more times ungreedy
\\2 # Match what was matched in group 2
\\s* # Match optionally some whitespaces
\\) # Match )', 'gs')
const str = `.someclass0 {
background: url('http://images/someimage.png') no-repeat;
}
@font-face {
font-family: 'FontAwesome';
src: url("fonts/fontawesome-webfont.eot?v=4.0.3");
src: url("fonts/fontawesome-webfont.eot?#iefix&v=4.0.3") format("embedded-opentype"), url("fonts/fontawesome-webfont.woff?v=4.0.3") format("woff"), url("fonts/fontawesome-webfont.ttf?v=4.0.3") format("truetype"), url("fonts/fontawesome-webfont.svg?v=4.0.3#fontawesomeregular") format("svg");
font-weight: normal;
font-style: normal;
}
.someclass1 {
background: url('images/someimage.png') no-repeat;
}
.someclass2 {
background: url("images/someimage.png") no-repeat;
}
.someclass3 {
background: url(images/someimage.png) no-repeat;
}`;
// Reset `lastIndex` if this regex is defined globally
// regex.lastIndex = 0;
let m;
while ((m = regex.exec(str)) !== null) {
// This is necessary to avoid infinite loops with zero-width matches
if (m.index === regex.lastIndex) {
regex.lastIndex++;
}
// The result can be accessed through the `m`-variable.
m.forEach((match, groupIndex) => {
console.log(`Found match, group ${groupIndex}: ${match}`);
});
}
Please keep in mind that these code samples are automatically generated and are not guaranteed to work. If you find any syntax errors, feel free to submit a bug report. For a full regex reference for JavaScript, please visit: https://developer.mozilla.org/en/docs/Web/JavaScript/Guide/Regular_Expressions