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:
When you visit a webpage, your browser sends the user-agent string to the server hosting the site that you are visiting. This string indicates which browser you're using, its version number, and details about your system, such as operating system and version. The web server can use this information to provide content that is tailored for your specific browser.
In JavaScript, the useragent string can be accessed using navigator.userAgent
.
/ipad/i
is a JavaScript Regular Expression literal that matches any string that contains ipad
(the i
at the end is an instruction to perform case insensitive comparision). The test()
method returns true if there was a match.
Regular expressions are available in many languages (PERL, PHP, JavaScript etc) and they are primarily used to match strings against simple to complicated patterns.
navigator
is a global object which contains information about the application running the script (e.g. the browser). navigator.userAgent contains the user agent string. For iPads, the user agent string looks like:
Mozilla/5.0 (iPad; CPU OS 4_3_2 like Mac OS X; en-us) AppleWebKit/533.17.9 (KHTML, like Gecko) Version/5.0.2 Mobile/8H7 Safari/6533.18.5
You will notice that it conains the word "iPad" in it.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With