0

What is the below syntax doing? More specifically, what exactly is the / and the i.test(navigator.userAgent)? Is this jquery stuff? Thanks!

    if(( /(ipad|iphone|ipod|android|windows phone)/i.test(navigator.userAgent) )) {

2 Answers 2

1

This:

/(ipad|iphone|ipod|android|windows phone)/i

is a regular expression literal. In this case, it's a expression that will match any of the substrings ipad, iphone, ipod, android, or windows phone. The i modifier at the end makes it case-insensitive.

This:

.test(navigator.userAgent)

is calling the test() method on that object. So it's checking whether navigator.userAgent contains any of the strings mentioned above.

Sign up to request clarification or add additional context in comments.

Comments

0

navigator.userAgent gives you the string that hold the details of browser, OS etc. Something like this

Mozilla/5.0 (Windows NT 6.1) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/40.0.2214.111 Safari/537.36

And the regex detect if one of if them is found in the string or not. If found the condition become true else false.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.