/ipad/i.test() how syntax works?

前端 未结 3 1383
生来不讨喜
生来不讨喜 2021-02-14 08:29

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

相关标签:
3条回答
  • 2021-02-14 08:37

    /ipad/i is Regex. ipad is taken literally, but the i will make the whole string case-insensitive. .test will test the given string to see if it satisfies the regex. navigator.userAgent is a string that browsers give identifying themselves (like "Mozilla/5.0 (iPad; CPU OS 6_0 like Mac OS X) AppleWebKit/536.26 (KHTML, like Gecko) Version/6.0 Mobile/10A5355d Safari/8536.25"). "toLowerCase" does exactly that.

    Using both /i (case-insensitive), and toLowerCase is redundant. I suggest just using navigator.userAgent.indexOf('iPad') !== -1, since iPad always has the same capitalization.

    0 讨论(0)
  • 2021-02-14 08:41

    /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.

    0 讨论(0)
  • 2021-02-14 08:47

    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.

    0 讨论(0)
提交回复
热议问题