Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations Chris Miller on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

reg exp help 1

Status
Not open for further replies.

GUJUm0deL

Programmer
Jan 16, 2001
3,676
US
Hi all, need some help with reg-exp. i haven't done reg-exp in a looooong time so i'm a bit rusty. This is what I have so far (and it works great):
Code:
^(Mozilla/4.[0-9][0-9]?)(.*)(MSIE[ \t]*(([5-9].[5-9])|([6-9].[0-9])))(.*)(Windows(.*))$||^(Mozilla/5.[0-9][0-9]?)(.*)(Macintosh(.*)|Windows(.*))((Netscape(.*)/([7].[1-9])(.*))|(Firefox/[1-9].[0-9](.*)))||^(Mozilla/[4-5].[0-9][0-9]?)()(.*)(Macintosh(.*))(Safari/[1-9](.*))||^(Mozilla/[0-9].[0-9][0-9]?)()(.*)((MSIE[ \t](.*))&(AOL[ \t](.*))&(Mac(.*)))||^(Mozilla/[4-9].[0-9][0-9]?)()(.*)((MSIE[ \t](.*))&(AOL[ \t][7-9](.*))&(Windows(.*)))

But now I need to add the following to the "list". The new addition is for iPhone browsers:
Code:
Mozilla/5.0 (iPhone; U; CPU like Mac OS X; en) AppleWebKit/420+ (KHTML, like Gecko) Version/3.0 Mobile/1A543a Safari/419.3

Can anyone help?

____________________________________
Just Imagine.
 
Looks like it'd be easiest just to compare the string. That's a hairy dog right there.

[monkey][snake] <.
 
I am a sucker for punishment:
Code:
var l_sUA = 'Mozilla/5.0 (iPhone; U; CPU like Mac OS X; en) AppleWebKit/420+ (KHTML, like Gecko) Version/3.0 Mobile/1A543a Safari/419.3';
var match = /(Mozilla\/5.[0-9][0-9]?)\s\(iPhone;(.*)\)\s(AppleWebKit\/420\+)(.*)\s(Safari\/[4-9][0-9][0-9]\.[0-9])$/i.test(l_sUA);
alert(match);
That will match - and includes some fuzzy logic to support some future-proofing. It's basically a starting point for you - based much on what you had. You would extend the existing regexp using || (as the others are).

Let me know if you get stuck changing it.

Cheers,
Jeff


[tt]Jeff's Page [!]@[/!] Code Couch
[/tt]

Make sure your web page and css validates properly against the doctype you have chosen - before you attempt to debug a problem!

FAQ216-6094
 
BabyJeffy, thanks. Can you tell me what this part does: $/i.test(l_sUA);

This is what I think is happening in your example: you're first taking my request and putting that into a string. Then in another var, you're using reg exp for the iPhone browsers. If that's right, then what's the purpose of $/i.test(l_sUA);?

____________________________________
Just Imagine.
 
The regexp starts with /^ and ends with $/ -- this indicates that it has to match from the start (^) to the end ($) of the string. The [!]i[/!] that follows the $/ indicates that the regexp is to consider the string it is testsing as case insensitive... and the .test part is a method that attempts to see if the string (which is in the variable l_sUA) fits the regexp (which sits before the .test() method).

It's hardly the most intuitive of things to do - I still take it slowly and refer to online pages as much as possible.

Another way to do this would be to create the regexp as a RegExp object - and then use test() or match() or whatever. I used test() purely to show it working.

Cheers,
Jeff


[tt]Jeff's Page [!]@[/!] Code Couch
[/tt]

Make sure your web page and css validates properly against the doctype you have chosen - before you attempt to debug a problem!

FAQ216-6094
 
Thanks for the reply. But this turned out to be very simple. This is what I needed:

Code:
 ^(Mozilla/4.[0-9][0-9]?)(.*)(MSIE[ \t]*(([5-9].[5-9])|([6-9].[0-9])))(.*)(Windows(.*))$||^(Mozilla/5.[0-9][0-9]?)(.*)(Macintosh(.*)|Windows(.*))((Netscape(.*)/([7].[1-9])(.*))|(Firefox/[1-9].[0-9](.*)))||^(Mozilla/[4-5].[0-9][0-9]?)()(.*)(Macintosh(.*))(Safari/[1-9](.*))||^(Mozilla/[0-9].[0-9][0-9]?)()(.*)((MSIE[ \t](.*))&(AOL[ \t](.*))&(Mac(.*)))||^(Mozilla/[4-9].[0-9][0-9]?)()(.*)((MSIE[ \t](.*))&(AOL[ \t][7-9](.*))&(Windows(.*)))||^( Mozilla/5.0 (iPhone; U; CPU like Mac OS X; en))

I simulated this in FF (using the User Agent Switcher extension) and I get the desired effect.

Thanks!

____________________________________
Just Imagine.
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top