What part of this RegEx is causing it to find no matches in IE7 and IE8?

我只是一个虾纸丫 提交于 2019-12-24 04:06:23

问题


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 )

回答1:


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 ?

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;


来源:https://stackoverflow.com/questions/8068027/what-part-of-this-regex-is-causing-it-to-find-no-matches-in-ie7-and-ie8

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!