2013-03-19 203 views
0

我必須提取標籤的內容之間的內容,這裏是我有:獲取標籤

<?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... 
+0

感覺就像它的時間來鏈接到最有名的答案在SO,再 - > http://stackoverflow.com/questions/1732348/regex-match- open-tags-except-xhtml-self-contained-tags – adeneo 2013-03-19 18:06:16

+0

如果這是在瀏覽器中,爲什麼不讓瀏覽器爲[你]做這件事(http://stackoverflow.com/questions/649614/xml-parsing-of-一個變量串式的JavaScript)。如果它的nodejs使用[libary](https://github.com/polotek/libxmljs/wiki/Element) – user1937198 2013-03-19 18:12:36

+0

感謝您的回覆。我使用mootools,我不知道.find的等價物。我在文檔中看不到任何東西 – 2013-03-19 18:20:23

回答

0

嘗試如果不存在匹配

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

匹配返回一個數組,或null
數組的第一個元素是整個匹配,第二個元素是由第一個捕獲組()匹配的文本。

.與換行符不匹配,但[\s\S]

+0

非常感謝你!它完美的作品! – 2013-03-19 18:43:01

0

使用解析XML的方法從here

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;