5 Replies Latest reply on Jan 8, 2014 12:01 PM by Lane Goolsby

    Question about RegExp and escaping ()

    Lane Goolsby

      It seems that I am as bad at regex as I think I am. I am trying to match to a string that starts with a set of parenthesis with any number in them. For example:

       

      var patt = new RegExp("^\([0-9]\)");
      var text = "(1)"
      if(patt.test(text)){
          console.println("found it");
      }
      else
      {
           console.println("did not find it");
      }
      

       

      Should produce these types of results:

       

      StringExpected ResultActual Result
      (1) Something Somethingtruefalse
      (9) truefalse
      Something Somethingfalsefalse
      (a)falsefalse
      (22)truefalse
      (a) Somethingfalsefalse
      99 Somethingfalsetrue
      Something (99) Somethingfalsefalse

       

      However, it seems that the escapes for the ()'s are not working as I get true for any string that starts with a number but not when the number is enclosed in (). Should I double escape the () in the expression?

       

      As I understand regex (and I openly admit you would be hard pressed to find something who knows it less than me), ^ should denote the start of the string, \( should escape the ( to say I want to look for a literal (, [0-9] should match any number, and again \) should mean a literal ).