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/
答案 0 :(得分:2)
你可以这样做:
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 "";
});
答案 1 :(得分:2)
获取所有子匹配的另一种方法。
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
答案 2 :(得分:0)
您可以按照以下方式执行此操作:
/\}(.*?)\{/g
第一个捕获的组$1
就是您想要的。它将包含here is html code
和here is css code