这个RegEx的哪个部分导致它在IE7和IE8中找不到匹配项? [英] What part of this RegEx is causing it to find no matches in IE7 and IE8?
问题描述
我有一个正则表达式,查找< span>
,类名为 foobar
。它适用于IE9,Chrome和Firefox。它在IE7和IE8中失败了。谁能告诉我为什么?
I've got a regex that looks for a <span>
with the class name foobar
. It works in IE9, Chrome and Firefox. It's failing in IE7 and IE8. Can anyone tell me why?
new RegExp( /(\s*?)<span\b(?:.*?)(?:class=(?:'|"|.*?\s)?foobar(?:\s|\3))(?:.*?)(?:\/)?>(.+?)<\/span>(\s*?)/g )
推荐答案
如果您正在对来自getElementById()。innerHTML或jQuery .html()的html应用正则表达式,那么IE将大写HTML标签,这可能是问题。如果你制作正则表达式不区分大小写 - 也就是说,最后将修饰符 / g
更改为 / gi
- 这样可以解决吗?
If you're applying the regex against html you got from getElementById().innerHTML or jQuery .html(), IE will uppercase the HTML tags which could be the problem. If you make the regex case-insensitive - that is, change the modifier /g
to /gi
at the end - does that fix it ?
看起来你也正在混合使用正则表达式文字和RegExp对象语法,你可以只使用正则表达式文字。
It seems like you're also mixing the regex literal and RegExp object syntax, you can do it with just the regex literal instead.
var regex = /(\s*?)<span\b(?:.*?)(?:class=(?:'|"|.*?\s)?foobar(?:\s|\3))(?:.*?)(?:\/)?>(.+?)<\/span>(\s*?)/gi;
这篇关于这个RegEx的哪个部分导致它在IE7和IE8中找不到匹配项?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!