I can't understand this code. If this is a RegExp, can it be done in a simpler way? Or is this already widely compatible? (with IE6 and newer browsers)
我无法理解这段代码。如果这是一个RegExp,可以用更简单的方式完成吗?或者这已经广泛兼容? (使用IE6和更新的浏览器)
var u = navigator.userAgent;
// Webkit - Safari
if(/webkit/i.test(u)){
// Gecko - Firefox, Opera
}else if((/mozilla/i.test(u)&&!/(compati)/.test(u)) || (/opera/i.test(u))){
}
Is this just:
这只是:
String.indexOf("webkit")
5 个解决方案
#1
First it looks for "webkit" (ignoring case) in the string u
in an attempt to determine that the browser is Safari.
首先,它在字符串u中查找“webkit”(忽略大小写),以确定浏览器是Safari。
If it doesn't find that, it looks for "mozilla" (without "compati") or "opera" in an attempt to determine that the browser is Firefox or Opera. Again, the searches are ignoring case (/i
).
如果没有找到,它会查找“mozilla”(没有“兼容”)或“opera”以试图确定浏览器是Firefox或Opera。同样,搜索忽略大小写(/ i)。
EDIT
The /.../i.test()
code is a regular expression, these are built into JavaScript.
/.../ i.test()代码是一个正则表达式,它们内置于JavaScript中。
#2
test() in javascript is a regular expression test function. You can read more about it here.
javascript中的test()是一个正则表达式测试函数。你可以在这里读更多关于它的内容。
This method tests for a match of a regular expression in a string, returning true if successful, and false if not. The test method can be used with either a string literal or a string variable.
此方法测试字符串中正则表达式的匹配,如果成功则返回true,否则返回false。测试方法可以与字符串文字或字符串变量一起使用。
Code:
rexp = /er/
if(rexp.test("the fisherman"))
document.write("It's true, I tell you.")
Output:
It's true, I tell you.
Also here's another great page that goes into more detail on this function.
此外,这是另一个很好的页面,详细介绍了这个功能。
Executes the search for a match between a regular expression and a specified string. Returns true or false.
执行搜索正则表达式与指定字符串之间的匹配项。返回true或false。
#3
This is similar, but returns a client name and version for any browser.
这类似,但返回任何浏览器的客户端名称和版本。
window.navigator.sayswho= (function(){
var N= navigator.appName, ua= navigator.userAgent, tem;
var M= ua.match(/(opera|chrome|safari|firefox|msie)\/? *(\.?\d+(\.\d+)*)/i);
if(M && (tem= ua.match(/version\/([\.\d]+)/i))!= null) M[2]= tem[1];
M= M? [M[1], M[2]]: [N, navigator.appVersion, '-?'];
return M;
})();
alert(navigator.sayswho)
#4
You code seems to be some kind of browser sniffing. The value of u
is probalby the user agent identifier. And it’s tested with regular expressions (build using the RegExp literal syntax /
expr
/
).
您的代码似乎是某种浏览器嗅探。 u的值是用户代理标识符的probal。并且它使用正则表达式进行测试(使用RegExp文字语法/ expr /构建)。
#5
Method test of the regular expressions tests for a match of a regular expression in a string, returning true if successful, and false if not.
正则表达式的方法测试测试字符串中正则表达式的匹配,如果成功则返回true,否则返回false。
this code tests for a match of the regular expressions 'webkit', 'mozilla' etc in the string in u variable.
此代码测试u变量中字符串中正则表达式'webkit','mozilla'等的匹配。
#1
First it looks for "webkit" (ignoring case) in the string u
in an attempt to determine that the browser is Safari.
首先,它在字符串u中查找“webkit”(忽略大小写),以确定浏览器是Safari。
If it doesn't find that, it looks for "mozilla" (without "compati") or "opera" in an attempt to determine that the browser is Firefox or Opera. Again, the searches are ignoring case (/i
).
如果没有找到,它会查找“mozilla”(没有“兼容”)或“opera”以试图确定浏览器是Firefox或Opera。同样,搜索忽略大小写(/ i)。
EDIT
The /.../i.test()
code is a regular expression, these are built into JavaScript.
/.../ i.test()代码是一个正则表达式,它们内置于JavaScript中。
#2
test() in javascript is a regular expression test function. You can read more about it here.
javascript中的test()是一个正则表达式测试函数。你可以在这里读更多关于它的内容。
This method tests for a match of a regular expression in a string, returning true if successful, and false if not. The test method can be used with either a string literal or a string variable.
此方法测试字符串中正则表达式的匹配,如果成功则返回true,否则返回false。测试方法可以与字符串文字或字符串变量一起使用。
Code:
rexp = /er/
if(rexp.test("the fisherman"))
document.write("It's true, I tell you.")
Output:
It's true, I tell you.
Also here's another great page that goes into more detail on this function.
此外,这是另一个很好的页面,详细介绍了这个功能。
Executes the search for a match between a regular expression and a specified string. Returns true or false.
执行搜索正则表达式与指定字符串之间的匹配项。返回true或false。
#3
This is similar, but returns a client name and version for any browser.
这类似,但返回任何浏览器的客户端名称和版本。
window.navigator.sayswho= (function(){
var N= navigator.appName, ua= navigator.userAgent, tem;
var M= ua.match(/(opera|chrome|safari|firefox|msie)\/? *(\.?\d+(\.\d+)*)/i);
if(M && (tem= ua.match(/version\/([\.\d]+)/i))!= null) M[2]= tem[1];
M= M? [M[1], M[2]]: [N, navigator.appVersion, '-?'];
return M;
})();
alert(navigator.sayswho)
#4
You code seems to be some kind of browser sniffing. The value of u
is probalby the user agent identifier. And it’s tested with regular expressions (build using the RegExp literal syntax /
expr
/
).
您的代码似乎是某种浏览器嗅探。 u的值是用户代理标识符的probal。并且它使用正则表达式进行测试(使用RegExp文字语法/ expr /构建)。
#5
Method test of the regular expressions tests for a match of a regular expression in a string, returning true if successful, and false if not.
正则表达式的方法测试测试字符串中正则表达式的匹配,如果成功则返回true,否则返回false。
this code tests for a match of the regular expressions 'webkit', 'mozilla' etc in the string in u variable.
此代码测试u变量中字符串中正则表达式'webkit','mozilla'等的匹配。