gpt4 book ai didi

javascript - 如何使用正则表达式匹配 document.doctype.internalSubset 中的所有元素

转载 作者:行者123 更新时间:2023-12-02 10:51:04 25 4
gpt4 key购买 nike

通过使用 document.doctype.internalSubset,我有以下字符串,例如 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#" >

然后,我使用正则表达式提取结果:

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 ,我们将使用这个:/(?:<!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/

最佳答案

新解决方案

好像是最后一个*使表达式消耗整个字符串,即使返回值仅包含第一个匹配项。如果将表达式更改为 /(?:<!ENTITY(.*?)>)/g; ,然后您可以多次应用它。正则表达式对象将跟踪最后一个匹配的结束位置并从那里继续。所以:

var regex = /(?:<!ENTITY(.*?)>)/g;
var results = [];
while (result = regex.exec(str)) {
results.push(result[1]);
}
<小时/>

旧的和失败的解决方案

尝试使用result = str.match(regex);反而。来自 MDN docs :

If the regular expression includes the g flag, the method returns an Array containing all matches. If there were no matches, the method returns null.

来自同一文档的示例

var str = "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz";
var regexp = /[A-E]/gi;
var matches_array = str.match(regexp);

// matches_array now equals ['A', 'B', 'C', 'D', 'E', 'a', 'b', 'c', 'd', 'e']

关于javascript - 如何使用正则表达式匹配 document.doctype.internalSubset 中的所有元素,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/14874357/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com