javascript - How to match space, newline in regexp -
this question has answer here:
- how use dotall flag regex.exec() 4 answers
string content
<head> <meta charset="utf-8"> <title></title> <!--@require a.css--> <!--@require bower/jquery--> <!--@require /build/bundle--> <!-- @require bootstrap @require bootstrap.css @require bootstrap.font --> </head>
js
var content = ...; var reg = /<!--[\s\t\n]*?@require (.+?)[\s\t\n]*?-->/g; var r; while((r = reg.exec(content)) != null) { console.log(r[1]); }
output
a.css bower/jquery /build/bundle
it's not match last block, miss in reg expression.
(dot). matches character except newline , that's why last string not matching. can use [\s\s]*
or [^]
matching character. ( ref : matching multiline patterns )
var content = `<head> <meta charset="utf-8"> <title></title> <!--@require a.css--> <!--@require bower/jquery--> <!--@require /build/bundle--> <!-- @require bootstrap @require bootstrap.css @require bootstrap.font --> </head>`; var reg = /<!--[\s\t\n]*?@require ([\s\s]+?)[\s\t\n]*?-->/g; var r; while ((r = reg.exec(content)) != null) { console.log(r[1]); }
Comments
Post a Comment