获取标签之间的内容

时间:2013-03-19 18:02:58

标签: javascript regex

我必须提取标签的内容,这就是我所拥有的:

<?xml version="1.0" encoding="utf-8"?>
<string xmlns="http://tempuri.org/">iVBORw0KGgoAAAANSUhEUgAAAU0AAAD6CAYAAAAlSBW9AAAAAXNSR0IArs4c6QAAAARnQU1BAACxjwv8YQUAAAAJcEhZcwAADsMAAA7DAcdvqGQAAP+lSURBVHhehP1lnB3H1a4P75Ely8zMzOyYHcccjsNOHNtx7IDtJCbJgtEwM4tG0kgzghnRaEgMI2aWLNlisMVMlgxa73Wv2tvJ85xz ...
</string>

这就是我想要的:

iVBORw0KGgoAAAANSUhEUgAAAU0AAAD6CAYAAAAlSBW9AAAAAXNSR0IArs4c6QAAAARnQU1BAACxjwv8YQUAAAAJcEhZcwAADsMAAA7DAcdvqGQAAP+lSURBVHhehP1lnB3H1a4P75Ely8zMzOyYHcccjsNOHNtx7IDtJCbJgtEwM4tG0kgzghnRaEgMI2aWLNlisMVMlgxa73Wv2tvJ85xz ...

我试过这个正则表达式:

iframeContents.match(/<string[^>]*>(.*?)<\/string>/i);

但是我一直得到字符串标签而不再是xml:

<string xmlns="http://tempuri.org/">iVBORw0KGgoAAAANSUhEUgAAAU0AAAD6CAYAAAAlSBW9AAAAAXNSR0IArs4c6QAAAARnQU1BAACxjwv8YQUAAAAJcEhZcwAADsMAAA7DAcdvqGQAAP+lSURBVHhehP1lnB...

2 个答案:

答案 0 :(得分:0)

尝试

iframeContents.match( /<string[^>]*>([\s\S]*?)<\/string>/i )[1]

匹配返回一个数组,如果没有匹配则返回null 数组的第一个元素是整个匹配,第二个元素是第一个捕获组()匹配的文本。

.与换行符不匹配,但[\s\S]会换行。

答案 1 :(得分:0)

使用here

中的解析xml方法
var parseXml;

if (typeof window.DOMParser != "undefined") {
    parseXml = function(xmlStr) {
        return ( new window.DOMParser() ).parseFromString(xmlStr, "text/xml");
    };
} else if (typeof window.ActiveXObject != "undefined" &&
       new window.ActiveXObject("Microsoft.XMLDOM")) {
    parseXml = function(xmlStr) {
        var xmlDoc = new window.ActiveXObject("Microsoft.XMLDOM");
        xmlDoc.async = "false";
        xmlDoc.loadXML(xmlStr);
        return xmlDoc;
    };
} else {
    throw new Error("No XML parser found");
})

var xml = parseXml("...");
var text = xml.documentElement.firstChild.nodeValue;