function getContentInBody(stype){
var html=document.body.innerHTML;
switch(stype){
case "html":
return html;
break;
return html.replace(/<.*?>/gi,"");
case "text":
break
}
}
var html=document.body.innerHTML;
switch(stype){
case "html":
return html;
break;
return html.replace(/<.*?>/gi,"");
case "text":
break
}
}
<script>
var a='<body> 文本内容 </body> <body> <BODY> <font style="color:red"> bbb </font> </BODY> </body>'
alert(a.replace(/^<body>(.*?)<\/body>.*$/i,"$1"))
alert(a.replace(/^.*?<body> *<BODY>(.*?)<\/BODY> *<\/body>$/i,"$1"))
</script>
这不是html document,是可扩展出席信息协议XMPP的格式,不过这里我暂时只当它字符串处理了,其实是个
XMLDocument
下面是实际接收到的一段XMLDoc,提供了读的2个接口,可以接受为XMLDocument<message from="[email protected]/resource1" id="rm324_69" to="[email protected]/#2285AT2008-1-28 10:12:14" type="chat"><body>文本消息</body><html xmlns="http://jabber.org/protocol/xhtml-im"><body xmlns="http://www.w3.org/1999/xhtml"><BODY><FONT color="#0000ff" face=""华文行楷"" size="5">文本消息</FONT></BODY></body></html><x xmlns="jabber:x:event"><composing/></x></message>也可以接受为字符串 var strMessage = '<message from="[email protected]/resource1" id="rm324_69" to="[email protected]/#2285AT2008-1-28 10:12:14" type="chat"><body>文本消息</body><html xmlns="http://jabber.org/protocol/xhtml-im"><body xmlns="http://www.w3.org/1999/xhtml"><BODY><FONT color="#0000ff" face=""华文行楷"" size="5">文本消息</FONT></BODY></body></html><x xmlns="jabber:x:event"><composing/></x></message>'只要能顺利拿到普通文本的信息和带格式的文本信息就可以了,加点分吧,提高大家的奋斗精神
<script>
var a='<body xmlns="http://www.w3.org/1999/xhtml"> 文本内容 </body> <body> <BODY> <font style="color:red"> bbb </font> </BODY> </body>'
alert(a.replace(/^<body.*?>(.*?)<\/body>.*$/i,"$1"))
alert(a.replace(/^.*?<body.*?> *<BODY.*?>(.*?)<\/BODY> *<\/body>$/i,"$1"))
</script>
<script>
var a='<body aa> 文本内容 </body> <body style="123"> <BODY style="123"> <font style="color:red"> bbb </font> </BODY> </body>'
alert(a.replace(/^<body[^>]*?>(.*?)<\/body[^>]*?>.*$/i,"$1"))
alert(a.replace(/^.*?<body[^>]*?> *<BODY[^>]*?>(.*?)<\/BODY[^>]*?> *<\/body[^>]*?>$/i,"$1"))
</script>
mingxuan3000 之前给你的,稍微改动下就完全可以!
# hanpoyangtitan
=======================
我竟然发现,即使在HTML里的,竟然是可以有2个body的,偶然发现的,但是符合不符合标准就不清楚。
有没有朋友提供一些w3c Doc的解决方法呢?
导入prototype1.6.js
<script src="prototype1.6.js"></script>
...var strMessage = ' <message from="[email protected]/resource1" id="rm324_69" to="[email protected]/#2285AT2008-1-28 10:12:14" type="chat"> <body> 文本消息 </body> <html xmlns="http://jabber.org/protocol/xhtml-im"> <body xmlns="http://www.w3.org/1999/xhtml"> <BODY> <FONT color="#0000ff" face=""华文行楷"" size="5"> 文本消息2 </FONT> </BODY> </body> </html> <x xmlns="jabber:x:event"> <composing/> </x> </message> ' //arr是一个数组,用于装结果
var arr = [];
strMessage.scan(/(>)(.*?)(<)/, function(match){ if(!match[2].blank()) arr.push(match[2])});
alert(arr.inspect())
结果:['文本消息 ','文本消息2']