0

Examples can be found at these locations:

JavaScript how to check User Agent for Mobile/Tablet

Detecting a mobile browser

For the problem I am working on, I will not be using Javascript to detect the user agent, so the answer does not directly affect my problem. However, while searching for the correct user agents, I kept seeing "/iPad/i", "/iPhone/i", "/Android/i" and so on. What does the "/i" mean after "/iPad"? Is it simply a regular expression, and to tell the Javascript function to be case insensitive?

I tried googling for userAgent.match() to get more information about the function itself, and to see what arguments it accepts, but I had no luck.

To elaborate: I've read a few sites that say user agent sniffing is bad news, so if you're wondering why I'm sniffing user agent, it's to compile a list of email addresses. Mac uses commas, Windows uses semi-colons. I recently became aware that iOS also uses commas, so my program was breaking on iPad. So, I need to update my user agent sniffing code.

Tiffany
  • 680
  • 1
  • 15
  • 31

2 Answers2

3

It meant as case insensitive

so "/iPhone/i" will match with iphone, IPHONE or IpHOne

Görkem D.
  • 551
  • 4
  • 13
0

You can pass a regular expression to match in 2 ways, either by creating a new instance of a RegExp object:

var re = new RegExp('pattenToMatch', 'i');
string.match(re);

or by using the shorthand as mentioned above - notice there are no double quotes surrounding a shorthand regex:

string.match(/iPhone/i);

In the first example, the second argument will contain any flags - eg i for case insensitive, g for global. In the second example, any flags are included after the last slash.

Joe Fitter
  • 1,309
  • 7
  • 11
  • There doesn't appear to be any difference, other than the shorthand is less typing. Does the second example not allow more than one flag? – Tiffany Jan 19 '16 at 16:20
  • first example allows you to use a variable as part of your regex. eg var re = new RegExp('\S+' + variable + '\S+', 'g'); You aren't able to achieve this with the shorthand way. – Joe Fitter Jan 19 '16 at 16:23
  • Ah, that makes sense, thanks for the clarification! – Tiffany Jan 19 '16 at 20:23