By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
459,566 Members | 1,785 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 459,566 IT Pros & Developers. It's quick & easy.

Regexp : invalid quantifier +

P: n/a
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 ?
--
Une Bévue
Jun 27 '08 #1
Share this Question
Share on Google+
6 Replies


P: n/a
Une Bévue wrote:
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/[^ ]+\\+ ");

--
Bart
Jun 27 '08 #2

P: n/a
Bart Van der Donck <ba**@nijlen.comwrote:
'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)
???
--
Une Bévue
Jun 27 '08 #3

P: n/a
Une Bévue wrote:
Bart Van der Donck <b...@nijlen.comwrote:
>'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.

--
Bart
Jun 27 '08 #4

P: n/a
On Jun 4, 12:00*pm, unbewusst.s...@weltanschauung.com.invalid (Une
Bévue) wrote:
>
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.

---
Geoff
Jun 27 '08 #5

P: n/a
Geoffrey Summerhayes <su*****@gmail.comwrote:
>
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 !
--
Une Bévue
Jun 27 '08 #6

P: n/a
Bart Van der Donck <ba**@nijlen.comwrote:
>
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 !

--
Une Bévue
Jun 27 '08 #7

This discussion thread is closed

Replies have been disabled for this discussion.