Stephen Jenkins
Stephen Jenkins

Reputation: 1836

RegExp negative lookahead to exclude string

I'm working on a small script that avoids IEMobile but detects the MSIE version, in this case IE9. I'm trying to use negative lookahead in the RegExp to do this, but it's not working and I'm wondering if I'm using negative lookahead correctly? Any advice appreciated.

/^(MSIE 9.0(?!IEMobile))*$/i

The UA String of IE Mobile:

Mozilla/5.0 (compatible; MSIE 9.0; Windows Phone OS 7.5; Trident/5.0; IEMobile/9.0; SAMSUNG; SGH-i917)

Upvotes: 0

Views: 451

Answers (1)

Tibos
Tibos

Reputation: 27823

First of all you don't need to anchor the regexp (so no ^ $ symbols at start and end).

Second of all, your regexp looks for IEMobile right after the MSIE 9.0, when in your actual example they are farther apart.

Third of all, . means any character. If you want version 9.0, you should use 9\.0 in your regexp.

The correct regexp should be:

/MSIE 9\.0(?!.*IEMobile)/i // matches IE9, but not IEMobile

Upvotes: 2

Related Questions