[英]Javascript regexp match doesn't work
var code = '{html}here is html code{/html}{css}here is css code{/css}';
var preg = code.match(new RegExp(/\{.*?\}(.*?)\{\/.*?\}/g));
console.log(preg[1][0]);
結果:{。
如何輸出“ HTML代碼在這里”和“ CSS代碼在這里”?
jsfiddle: http : //jsfiddle.net/cqr3K/
你可以這樣做:
var code = '{html}here is html code{/html}{css}here is css code{/css}';
var preg = code.replace(/\{.*?\}(.*?)\{\/.*?\}/g, function (match,group) {
console.log(group);
return "";
});
獲取所有子匹配項的另一種方法。
var code = '{html}here is html code{/html}{css}here is css code{/css}';
var re = /[^}]+(?=\{)/g;
var match;
while (match = re.exec(code)) {
console.log(match[0]);
}
產量
here is html code
here is css code
您可以按照以下步驟進行操作:
/\\}(.*?)\\{/g
第一個捕獲的組$1
是您想要的。 它將包含here is html code
和here is css code
演示: http : //regex101.com/r/yC7lD7
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.