I'm trying to replace all @import strings in one single-string, e.g.
var str = 'buttons .reprint{height:56px;background-position:-47px -1496px;}@import "../common/buttons.css";@import "../screen/screen-1.css";@import "../screen/screen-2.css";@import "../screen/screen-3.css";@import "../screen/screen-5.css";@import "../screen/screen-6.css";@import "../screen/screen-7.css";@import "../screen/screen-quick-messages.css";@import "../screen/reset-password.css";#xml-data{display:none;}body{font-family:"Helvetica Neue",HelveticaNeue,Helvetica-Neue,Helvetica,sans-serif;}ul{padding:0;}body,section{width:768px;}';
It works if I write:
console.log(str.match(/@import\ /g));
But how to specify, all @import declaration at all in single line, e.g. @import "../screen/screen-1.css";
console.log(str.match(/@import\ .+;/g));
doesn't work.
.+"didn't work", the type of "didn't work" I can think of is that it matched too much, because + is greedy, meaning it would go to the last semicolon it found. Adding?to a quantifier makes it becomes non-greedy - so/@import\ .+?;/gwould work. But you could potentially have semicolons in the URL, so Tim's answer is obviously much better. Just wanted to explain what was wrong in case it helps another time. But do be as specific as you can always, it avoids errors.