匹配使用document.doctype.internalSubset,我有以下字符串,說STR:如何使用正則表達式的所有元素document.doctype.internalSubset
<!ENTITY owl "http://www.w3.org/2002/07/owl#" >
<!ENTITY xsd "http://www.w3.org/2001/XMLSchema#" >
<!ENTITY rdfs "http://www.w3.org/2000/01/rdf-schema#" >
<!ENTITY rdf "http://www.w3.org/1999/02/22-rdf-syntax-ns#" >
然後,我用正則表達式提取結果:
result = regex.exec(str);
我預期輸出是其中的數組:
result[0] = owl "http://www.w3.org/2002/07/owl#"
result[1] = xsd "http://www.w3.org/2001/XMLSchema#"
...
result[3] = rdf "http://www.w3.org/1999/02/22-rdf-syntax-ns#"
所以,我創建這個正則表達式:
var regex = /(?:<!ENTITY(.*?)>)*/g;
,這裏是結果,當然,這不是我想要的:
owl "http://www.w3.org/2002/07/owl#"
誰能幫助我找出錯誤,以及如何解決所有這些問題?
注意,我可以使用s.indexOf()得到的<!ENTITY
和>,然後的位置,使用s.subString()得到相同的結果,但我現在學習正則表達式,所以我想使用正則表達式。
--------------更新---------------
感謝SUPR,我終於可以計算出錯誤,在我看來,在這種情況下,「*」並不意味着「匹配一次或多次」,所以我們將使用這一個:/(?:<!ENTITY(.*?)>)/g
(禁止*),然後循環字符串,直到我們得到所有結果。這裏是源:
var str = '<!ENTITY owl "http://www.w3.org/2002/07/owl#" >'
+ '<!ENTITY xsd "http://www.w3.org/2001/XMLSchema#" >'
+ '<!ENTITY rdfs "http://www.w3.org/2000/01/rdf-schema#" >'
+ '<!ENTITY rdf "http://www.w3.org/1999/02/22-rdf-syntax-ns#" >'
var regex = /(?:<!ENTITY(.*?)>)/g;
var results = [];
while ((result = regex.exec(str)) != null) {
results.push(result[1]);
}
console.log(str);
console.log("-------------------------------");
for (var i = 0; i < results.length; i++) {
document.write(results[i] + "<br>");
}
來進行測試:http://jsfiddle.net/nxhoaf/jZpHv/
順便說一句,這裏是用s.indexOf()和遞歸我的解決方案:
var str = '<!ENTITY owl "http://www.w3.org/2002/07/owl#" >'
+ '<!ENTITY xsd "http://www.w3.org/2001/XMLSchema#" >'
+ '<!ENTITY rdfs "http://www.w3.org/2000/01/rdf-schema#" >'
+ '<!ENTITY rdf "http://www.w3.org/1999/02/22-rdf-syntax-ns#" >'
var getNamespace = function(input) {
var result = []; // Store the final result
var temp = []; // Store the temporary result
// Non trivial case
if ((input != null) && (input.length != 0)) {
// Get the begin and and index to extract the entity's content
var begin = input.indexOf("<!ENTITY");
var end = input.indexOf(">");
if ((begin == -1) || (end == -1) || (begin >= end)) { // not found
return null;
}
// Fix the begin index
begin = begin + "<!ENTITY".length;
// Get the content and save it to result variable
var item = input.substring(begin, end); // ok, get one item
// As end > begin, item is always != null
item = item.trim(); // Normalize
result.push(item);
// Continue searching with the rest using
// recursive searching
temp = getNamespace(input.substring(end + 1));
// Ok, collect all of data and then return
if (temp != null) {
for (var i = 0; i < temp.length; i++) {
result.push(temp[i]);
}
}
return result;
} else { // Trivial case
return null;
}
}
// Parse it to get the result
result = getNamespace(str);
console.log("*************");
for (var i = 0; i < result.length; i++) {
document.write(result[i] + "<br>");
}
你可以在這裏進行測試: http://jsfiddle.net/nxhoaf/FNFuG/
你告訴我們什麼你不想要,但沒有告訴我們你想要什麼 – aa8y 2013-02-14 12:13:31
Wh在我想要的是在「我的expexted結果......」:結果[0] =貓頭鷹「http://www.w3.org/2002/07/owl#」,等等... – nxhoaf 2013-02-14 13:05:29