I have JavaScript a code
var docBodyText = document.body.innerHTML;
var patt =/<!--:.+:-->/g;
patt.compile(patt);
var matchesB=docBodyText.match(patt)
and a HTML page
<body>
<span><b><!--:?time:--> <!--:?date:--></b></span>
<br />
<!--:?url:-->
</body>
When I execute the script it returns
["<!--:?time:--><!--:?date:-->","<!--:?url:-->"]
but I want
<!--:?time:-->
and <!--:?date:-->
to be returned separately.
You are matching greedily . The +
by default means "1 or more times, but prefer as many as possible". Add a ?
modifier after the +
to make it match lazily:
var patt =/<!--:.+?:-->/g;
尝试更改模式以排除其他>
标签:
var patt =/<!--:[^>]+:-->/g;
Javascript regex are greedy by default. You need to use the non-greedy qualifier for this to get the shortes matching string.
var patt =/<!--:.+?:-->/g;
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.