[英]RegExp uncaught TypeError: Cannot read property '1' of null
I found this code;我找到了这个代码;
navigator.appVersion.match(/MSIE ([\\d.]+)/)[1];
For detecting browser version.用于检测浏览器版本。 This code works in IE 8 below but not on google chrome.此代码适用于下面的 IE 8,但不适用于谷歌浏览器。 The error in google chrome says;谷歌浏览器中的错误说;
Uncaught TypeError: Cannot read property '1' of null
and it points to the line where this code belongs;它指向此代码所属的行;
navigator.appVersion.match(/MSIE ([\\d.]+)/)[1];
Any idea how to fix this issue?知道如何解决这个问题吗?
just to make things clear what i'm trying to accomplish here is to detect the browser version:只是为了弄清楚我在这里要完成的是检测浏览器版本:
var version = navigator.appVersion.match(/MSIE ([\d.]+)/)[1];
if(version <= 8.0)
{
execute code;
}
Everything works fine in IE but I got an error in google chrome which is:在 IE 中一切正常,但在 google chrome 中出现错误,即:
Uncaught TypeError: Cannot read property '1' of null未捕获的类型错误:无法读取 null 的属性“1”
and it points to the line where this code belongs;它指向此代码所属的行;
navigator.appVersion.match(/MSIE ([\\d.]+)/)[1]; navigator.appVersion.match(/MSIE ([\\d.]+)/)[1];
match
can return null
if regexp don't find anything.如果 regexp 没有找到任何东西, match
可以返回null
。 So you must first get matches and check have you any match or not.因此,您必须首先获得匹配项并检查您是否匹配。 Try this:尝试这个:
var ieMatches = navigator.appVersion.match(/MSIE ([\d.]+)/);
var isIE = !!ieMatches[1];
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.