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

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

提交回复
热议问题