问题描述
我是 /ipad/i.test(navigator.userAgent.toLowerCase())
语法的新手。我知道结果它对于ipad返回true而对于其余浏览器则返回false。
请任何正文解释 / ipad / i
它意味着什么以及如何运作
以下是 / ipad / i的简单细分。 test(navigator.userAgent.toLowerCase())
:
var myRegex = new RegExp(ipad , 一世);;
var result = myRegex.test(navigator.userAgent.toLowerCase());
这里 RegExp
是。 / p>
然后调用RegExp的 test()
方法,并将浏览器的useragent字符串传递给它。 test()方法尝试将useragent字符串与 ipad
匹配,如果找到 true
将被返回。 工作演示: 。
:
In JavaScript,可以使用 navigator.userAgent
访问useragent字符串。
I am new to /ipad/i.test(navigator.userAgent.toLowerCase())
syntax. I know the results it returns true for ipad and false for remaining browsers.
please any body explain /ipad/i
what it means and how it works
Here is a simpler breakdown of /ipad/i.test(navigator.userAgent.toLowerCase())
:
var myRegex = new RegExp("ipad", "i");;
var result = myRegex.test(navigator.userAgent.toLowerCase());
Here RegExp
is the constructor of JavaScript's RegExp object.
It creates a regular expression to match ipad
string using the i
flag which tells the RegExp object to ignore case of the string to be matched. Regular expressions are patterns used to match character combinations in strings.
Then the test()
method of RegExp is called and browser's useragent string in being passed to it. The test() method tries to match the useragent string with ipad
, if found true
will be returned. Working demo: http://jsfiddle.net/8mzTE/.
A user-agent string identifies your browser and provides its details:
In JavaScript, the useragent string can be accessed using navigator.userAgent
.
这篇关于/ipad/i.test()语法如何工作?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!