Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > Javascript > Regexp : invalid quantifier +

Reply
Thread Tools

Regexp : invalid quantifier +

 
 
Une Bévue
Guest
Posts: n/a
 
      06-04-2008
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
 
Reply With Quote
 
 
 
 
Bart Van der Donck
Guest
Posts: n/a
 
      06-04-2008
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
 
Reply With Quote
 
 
 
 
Une Bévue
Guest
Posts: n/a
 
      06-04-2008
Bart Van der Donck <(E-Mail Removed)> wrote:

> '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
 
Reply With Quote
 
Bart Van der Donck
Guest
Posts: n/a
 
      06-04-2008
Une Bévue wrote:

> Bart Van der Donck <(E-Mail Removed)> wrote:
>
>> '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
 
Reply With Quote
 
Geoffrey Summerhayes
Guest
Posts: n/a
 
      06-04-2008
On Jun 4, 12:00*pm, (E-Mail Removed) (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
 
Reply With Quote
 
Une Bévue
Guest
Posts: n/a
 
      06-04-2008
Geoffrey Summerhayes <(E-Mail Removed)> wrote:

>
> 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
 
Reply With Quote
 
Une Bévue
Guest
Posts: n/a
 
      06-04-2008
Bart Van der Donck <(E-Mail Removed)> wrote:

>
> 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
 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
[regexp] How to convert string "/regexp/i" to /regexp/i - ? Joao Silva Ruby 16 08-21-2009 05:52 PM
problems with regex matching - invalid ranges, quantifier follows nothing, etc. Jack Perl Misc 2 10-04-2006 12:53 AM
Quantifier...bigger than 32766...in regex leegee@gmail.com Perl Misc 19 05-07-2006 12:25 PM
Greedy quantifier - Java 0 07-11-2005 01:34 AM
Using a variable size with the repetition quantifier Philippe Aymer Perl Misc 12 10-14-2004 05:59 PM



Advertisments