Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/javascript/362.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/4/regex/16.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Javascript IE8无法识别我的正则表达式_Javascript_Regex_Internet Explorer 8 - Fatal编程技术网

Javascript IE8无法识别我的正则表达式

Javascript IE8无法识别我的正则表达式,javascript,regex,internet-explorer-8,Javascript,Regex,Internet Explorer 8,我试图解析一个AJAX响应并获取body标记的id。但是,我不能通过jQuery实现这一点,因为jQuery不能模拟body标记的DOM响应 我把它分成了许多行,试图找出错误。这在现代浏览器中有效,但在IE8中却失败了 var bodyIDregex = new RegExp(/<body[^>]*id=["'](.*?)["']>/gi), matched = html.match(bodyIDregex), bodyID = bodyIDregex.exec(matched

我试图解析一个AJAX响应并获取body标记的id。但是,我不能通过jQuery实现这一点,因为jQuery不能模拟body标记的DOM响应

我把它分成了许多行,试图找出错误。这在现代浏览器中有效,但在IE8中却失败了

var bodyIDregex = new RegExp(/<body[^>]*id=["'](.*?)["']>/gi),
matched = html.match(bodyIDregex),
bodyID = bodyIDregex.exec(matched[0]);
bodyID = bodyID[1];
var-bodyIDregex=new RegExp(/]*id=[“”](.*?[“]>/gi),
matched=html.match(bodyIDregex),
bodyID=bodyIDregex.exec(匹配[0]);
bodyID=bodyID[1];
我已经确认变量html的值与预期值相同

有什么帮助吗


谢谢

您应该将字符串传递给regexen的构造函数,或者使用regex文本语法,但不能同时使用这两种语法

var bodyIDregex = /<body[^>]*id=["'](.*?)["']>/gi
显然,当您调用(RegExp object).match(string)时,它会增加名为lastIndex的RegExp对象的属性。我并不完全熟悉RegExp对象的工作方式,但这会在稍后尝试调用exec()方法时引起问题

显然,解决办法是将lastIndex重置为零

var html = '<html><body id="test">asdf</body></html>';

var bodyIDregex = /<body[^>]*id=["'](.*?)["']>/gi,
matched = html.match(bodyIDregex);
// Reset lastIndex
bodyIDregex.lastIndex = 0;
var bodyID = bodyIDregex.exec(matched[0]);
alert(bodyID.length);
bodyID = bodyID[1];

document.write(bodyID); // writes test
var html='asdf';
var bodyIDregex=/]*id=[“”](.*?[“”]>/gi,
matched=html.match(bodyIDregex);
//重置最后索引
bodyIDregex.lastIndex=0;
var bodyID=bodyIDregex.exec(匹配[0]);
警报(车身ID.长度);
bodyID=bodyID[1];
document.write(bodyID);//写测试

很高兴知道,谢谢!不幸的是,它没有解决我的错误,仍然说1是空的或不是对象。@MMiller这确实很奇怪。这意味着IE8不会将捕获的组作为条目包含在matches数组中。您可以发布错误的确切文本吗?下面是WriteCodeOnline.com上的一个测试截图(我猜JSFIDLE在IE8中不起作用?)@MMiller为什么要对原始字符串执行两次正则表达式?你知道吗?我不知道为什么会这样。显示当我尝试使用正则表达式时发生的情况。。。。谢谢除了Asad所说的之外,我猜这与“g”标志以及您首先调用
.match(regex)
然后使用相同的正则表达式调用
regex.exec()
的顺序有关。“g”标志可以在正则表达式中保留状态,因为可以多次调用
.exec
,以生成连续的匹配。我建议您对
.match()
调用使用不同的正则表达式(可能没有“g”标志)。如果您包含匹配的示例文本,我可以验证是否存在这种情况,但如果没有示例文本,我们只是猜测。@paulsm4:错。jQuery确实存在这样的错误,它无法解析完整的HTML文档。还有很多事情jQuery甚至不是设计用来做的……如果jQuery被设置为将结果解释为XML文档而不是HTML文档,那么看看它是否能够解析body标记将是一件有趣的事情。。。但老实说,我也不确定这是否有效。当然,这里的限制是必须有一个有效XML(XHTML)的HTML文档。我用HTML5编写,有时候没有关闭斜杠,等等,这是XML文档所需要的。我用一种解决问题的方法更新了我的答案。
var bodyIDregex = /<body[^>]*id=["'](.*?)["']>/gi,
    bodyID = bodyIDregex.exec(html);

//bodyID is now the array, ["<body id="test">asdf</body>", "test"]

alert(bodyID[1]);
//alerts the captured group, "test"
var html = '<html><body id="test">asdf</body></html>';

var bodyIDregex = /<body[^>]*id=["'](.*?)["']>/gi,
matched = html.match(bodyIDregex);
// Reset lastIndex
bodyIDregex.lastIndex = 0;
var bodyID = bodyIDregex.exec(matched[0]);
alert(bodyID.length);
bodyID = bodyID[1];

document.write(bodyID); // writes test