这个正则表达式怎么解?

womakelovewithnima 2010-11-18 05:38:08
/<tpl\b[^>]*>((?:(?=([^<]+))\2|<(?!tpl\b[^>]*>))*?)<\tpl>/

用得是MATCH 方法,返回结果应该包括<tpl></tpl>这对标签吧,但是没包括这对标签的也返回了!

...全文
97 9 打赏 收藏 转发到动态 举报
写回复
用AI写文章
9 条回复
切换为时间正序
请发表友善的回复…
发表回复
兔子-顾问 2010-11-18
  • 打赏
  • 举报
回复
修改一下。
var reg = /<tpl\s[^>]*>[\s\S]*?<\/tpl>/g;
兔子-顾问 2010-11-18
  • 打赏
  • 举报
回复
写的比较乱。滥用环视。
<tpl></tpl>之间的很简单

var reg = /<tpl>[\s\S]*?<\/tpl>/g;
Mr-Jee 2010-11-18
  • 打赏
  • 举报
回复
嗯 是这样的
m[0]是你匹配的内容
m[1]是第一个子模式
m[2]是第二个子模式
womakelovewithnima 2010-11-18
  • 打赏
  • 举报
回复
是我在调试gridpanel时出现的,那段HTML代码不记得了。

while((m = s.match(re)))

调试到这里的时候,m的第一个值就是s,第二个值是s中去掉了这对标签<tpl></tpl>中的内容,第三个值是'/tpl>',太奇怪了,不知道是怎么匹配的?
Mr-Jee 2010-11-18
  • 打赏
  • 举报
回复
能够把不是tpl的而没有被匹配的发给我看看吗
womakelovewithnima 2010-11-18
  • 打赏
  • 举报
回复
源代码在这里
Ext.XTemplate = function(){
Ext.XTemplate.superclass.constructor.apply(this, arguments);

var me = this,
s = me.html,
re = /<tpl\b[^>]*>((?:(?=([^<]+))\2|<(?!tpl\b[^>]*>))*?)<\/tpl>/,
nameRe = /^<tpl\b[^>]*?for="(.*?)"/,
ifRe = /^<tpl\b[^>]*?if="(.*?)"/,
execRe = /^<tpl\b[^>]*?exec="(.*?)"/,
m,
id = 0,
tpls = [],
VALUES = 'values',
PARENT = 'parent',
XINDEX = 'xindex',
XCOUNT = 'xcount',
RETURN = 'return ',
WITHVALUES = 'with(values){ ';

s = ['<tpl>', s, '</tpl>'].join('');

while((m = s.match(re))){
var m2 = m[0].match(nameRe),
m3 = m[0].match(ifRe),
m4 = m[0].match(execRe),
exp = null,
fn = null,
exec = null,
name = m2 && m2[1] ? m2[1] : '';

if (m3) {
exp = m3 && m3[1] ? m3[1] : null;
if(exp){
fn = new Function(VALUES, PARENT, XINDEX, XCOUNT, WITHVALUES + RETURN +(Ext.util.Format.htmlDecode(exp))+'; }');
}
}
if (m4) {
exp = m4 && m4[1] ? m4[1] : null;
if(exp){
exec = new Function(VALUES, PARENT, XINDEX, XCOUNT, WITHVALUES +(Ext.util.Format.htmlDecode(exp))+'; }');
}
}
if(name){
switch(name){
case '.': name = new Function(VALUES, PARENT, WITHVALUES + RETURN + VALUES + '; }'); break;
case '..': name = new Function(VALUES, PARENT, WITHVALUES + RETURN + PARENT + '; }'); break;
default: name = new Function(VALUES, PARENT, WITHVALUES + RETURN + name + '; }');
}
}
tpls.push({
id: id,
target: name,
exec: exec,
test: fn,
body: m[1]||''
});
s = s.replace(m[0], '{xtpl'+ id + '}');
++id;
}
for(var i = tpls.length-1; i >= 0; --i){
me.compileTpl(tpls[i]);
}
me.master = tpls[tpls.length-1];
me.tpls = tpls;
};
Mr-Jee 2010-11-18
  • 打赏
  • 举报
回复
[Quote=引用 2 楼 hch126163 的回复:]

你想要匹配什么啊?

匹配:<tpl></tpl>
/<tpl\b[^>]*>.+?<\tpl>/
[/Quote]

呵呵
你这个正则有问题呀

var str = "<tpl>asdfasdfasdfasdf\n<\tpl>";
var i = /<tpl\b[^>]*>.+?<\tpl>/g;
alert(i.test(str));

hch126163 2010-11-18
  • 打赏
  • 举报
回复
你想要匹配什么啊?

匹配:<tpl></tpl>
/<tpl\b[^>]*>.+?<\tpl>/
Mr-Jee 2010-11-18
  • 打赏
  • 举报
回复
你正则是完全拷贝过来的嘛?怎么连<\/tpl>都被换成<\tpl>了啊

87,910

社区成员

发帖
与我相关
我的任务
社区描述
Web 开发 JavaScript
社区管理员
  • JavaScript
  • 无·法
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

试试用AI创作助手写篇文章吧