Regexp : invalid quantifier +

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • =?ISO-8859-1?Q?Une_B=E9v?==?ISO-8859-1?Q?ue?=

    Regexp : invalid quantifier +

    I wanted to test userAgent by a Regexp

    the goal :

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

    false otherwise

    then i wrote :

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

    var isWebkitNightly =re.test(naviga tor.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
  • Bart Van der Donck

    #2
    Re: Regexp : invalid quantifier +

    Une Bévue wrote:
    I wanted to test userAgent by a Regexp
    >
    the goal :
    >
    true if UA contains  "AppleWebKi t/528+ " or "AppleWebKi t/525.12+ "
    note the final "+"
    >
    false otherwise
    >
    then i wrote :
    >
    var re=new Regexp(" AppleWebKit/[^ ]+\+ ");
    >
    var isWebkitNightly =re.test(naviga tor.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

    Comment

    • =?ISO-8859-1?Q?Une_B=E9v?==?ISO-8859-1?Q?ue?=

      #3
      Re: Regexp : invalid quantifier +

      Bart Van der Donck <bart@nijlen.co mwrote:
      '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.inf o/javascriptexamp le.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.inpu t.value)
      ???
      --
      Une Bévue

      Comment

      • Bart Van der Donck

        #4
        Re: Regexp : invalid quantifier +

        Une Bévue wrote:
        Bart Van der Donck <b...@nijlen.co mwrote:
        >
        >'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.inf o/javascriptexamp le.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.inpu t.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

        Comment

        • Geoffrey Summerhayes

          #5
          Re: Regexp : invalid quantifier +

          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.inpu t.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

          Comment

          • =?ISO-8859-1?Q?Une_B=E9v?==?ISO-8859-1?Q?ue?=

            #6
            Re: Regexp : invalid quantifier +

            Geoffrey Summerhayes <sumrnot@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

            Comment

            • =?ISO-8859-1?Q?Une_B=E9v?==?ISO-8859-1?Q?ue?=

              #7
              Re: Regexp : invalid quantifier +

              Bart Van der Donck <bart@nijlen.co mwrote:
              >
              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

              Comment

              Working...