下面的代码提取标签之间的信息,可以应付一般的任务
var pt = function (s) {
document.writeln("<br>" + s);
};
var m = "wabc ss ab xx<div><h1>div</h1></div>ss<div>div2</div>";
function getTagContent(tg){
var str = "<" + tg + ">(?:(?!<\\/" + tg + ">).)*<\\/" + tg + ">";
var pttn = new RegExp(str,"g");
var tmp = this.match(pttn);
var r = [];
for(i=0;i<tmp.length;i++){
r.push(tmp[i].replace("<"+tg+">","").replace("</"+tg+">",""));
}
return r;
}
var y = getTagContent.call(m,"div");
alert(y);
pt(y);
不支持嵌套结构
输出: <h1>div</h1>,div2
原文地址:http://blog.51cto.com/idata/2337742
时间: 2024-10-12 22:57:00