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

前端 未结 3 1393
生来不讨喜
生来不讨喜 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: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.

提交回复
热议问题