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

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

提交回复
热议问题