Regexp : invalid quantifier +

U

Une Bévue

I wanted to test userAgent by a Regexp

the goal :

true if UA contains "AppleWebKit/528+ " or "AppleWebKit/525.12+ " note
the final "+"

false otherwise

then i wrote :

var re=new Regexp(" AppleWebKit/[^ ]+\+ ");

var isWebkitNightly=re.test(navigator.userAgent);

and i got the following error with Firefox3RC1 :

invalid quantifier +


however, testing that way :

var isWebkitNightly=/ AppleWebKit\/[^ ]+\+ /.test(navigator.userAgent);

works fine.

why ?
 
B

Bart Van der Donck

Une said:
I wanted to test userAgent by a Regexp

the goal :

true if UA contains  "AppleWebKit/528+ " or "AppleWebKit/525.12+ "
note the final "+"

false otherwise

then i wrote :

var re=new Regexp(" AppleWebKit/[^ ]+\+ ");

var isWebkitNightly=re.test(navigator.userAgent);

and i got the following error with Firefox3RC1 :

invalid quantifier +

however, testing that way :

var isWebkitNightly=/ AppleWebKit\/[^ ]+\+
/.test(navigator.userAgent);

works fine.

why ?

'new Regexp' should be 'new RegExp', and the last plus should be
escaped double.

var re=new RegExp(" AppleWebKit/[^ ]+\\+ ");
 
U

Une Bévue

Bart Van der Donck said:
'new Regexp' should be 'new RegExp',

yes, it's a typo of me )))
and the last plus should be
escaped double.

var re=new RegExp(" AppleWebKit/[^ ]+\\+ ");

i've found that too in the mean time )))

but if i enter the following (without quotes) :

" AppleWebKit/[^ ]+\+ " // NOT double escaped

in an input text field as in this RegExp tester :
<http://www.regular-expressions.info/javascriptexample.html>

it works fine, does that means, because it is in an input text, a second
escape (ie. \) is inserted, behind the scene, the text being supposed to
be POSted ??? and this second \ isn't seen by an alert(that.input.value)
???
 
B

Bart Van der Donck

Une said:
Bart Van der Donck said:
'new Regexp' should be 'new RegExp',

yes, it's a typo of me )))
and the last plus should be
escaped double.
  var re=new RegExp(" AppleWebKit/[^ ]+\\+ ");

i've found that too in the mean time )))

but if i enter the following (without quotes) :

" AppleWebKit/[^ ]+\+ " // NOT double escaped

in an input text field as in this RegExp tester :
<http://www.regular-expressions.info/javascriptexample.html>

it works fine, does that means, because it is in an input text, a second
escape (ie. \) is inserted, behind the scene, the text being supposed to
be POSted ??? and this second \ isn't seen by an alert(that.input.value)
???

It hasn't anything to do with the '+'. It is the backslash itself that
needs to be escaped, so a correct '\+' is received to perform the
regexp with. When the regexp is taken from an input box, the backslash
doesn't need to be escaped because it is then passed as the actual
character by itself.
 
G

Geoffrey Summerhayes

it works fine, does that means, because it is in an input text, a second
escape (ie. \) is inserted, behind the scene, the text being supposed to
be POSted ??? and this second \ isn't seen by an alert(that.input.value)
???

Got it backwards. The input text only gets parsed for
escape sequences in the regexp, however when typed in
as a javascript constant string it get parsed twice,
first by the javascript parser to create the 'correct'
string value, then by the regexp object.
 
U

Une Bévue

Geoffrey Summerhayes said:
Got it backwards. The input text only gets parsed for
escape sequences in the regexp, however when typed in
as a javascript constant string it get parsed twice,
first by the javascript parser to create the 'correct'
string value, then by the regexp object.

perfectly clear thanks !
 
U

Une Bévue

Bart Van der Donck said:
It hasn't anything to do with the '+'. It is the backslash itself that
needs to be escaped, so a correct '\+' is received to perform the
regexp with. When the regexp is taken from an input box, the backslash
doesn't need to be escaped because it is then passed as the actual
character by itself.

ok, clear enough, thanks !
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,768
Messages
2,569,574
Members
45,049
Latest member
Allen00Reed

Latest Threads

Top