Regular Expressions containing square brackets [ ]

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • hsriat
    Recognized Expert Top Contributor
    • Jan 2008
    • 1653

    Regular Expressions containing square brackets [ ]

    I need to compare a string with a regular expression that contains square brackets in itself.

    eg. If I test() a regExp ("^[b]", "i") with string "[b]Bold Text[/b]", it should return true. But its returning false due to square brackets.
    How can I solve this?

    [code=javascript]var txt = "[b]Page starts[/b]";
    function matchTag(start) {
    var regStart = new RegExp("^["+start+"]", "i");
    if (regStart.test( txt))
    alert ('Matching');
    }[/code]
  • acoder
    Recognized Expert MVP
    • Nov 2006
    • 16032

    #2
    Square brackets are special characters in regular expressions, so you will need to escape them.

    Comment

    • hsriat
      Recognized Expert Top Contributor
      • Jan 2008
      • 1653

      #3
      Originally posted by acoder
      Square brackets are special characters in regular expressions, so you will need to escape them.
      oops... how could I miss using escape().

      Thanks, I got it working. :)

      [code=javascript]var txt = "[b]Page starts[/b]";
      function matchTag(start) {
      var regStart = new RegExp("^"+esca pe("["+start+"]"), "i");
      if (regStart.test( escape(txt)))
      alert ('Matching');
      }[/code]

      Comment

      • acoder
        Recognized Expert MVP
        • Nov 2006
        • 16032

        #4
        Yes, or just the escape character (backslash).

        Comment

        • hsriat
          Recognized Expert Top Contributor
          • Jan 2008
          • 1653

          #5
          Originally posted by acoder
          Yes, or just the escape character (backslash).
          Are you talking about this?
          [code=javascript]var txt = "[b]Page starts[/b]";
          function matchTag(start) {
          var regStart = new RegExp("^\["+start+"\]", "i");
          if (regStart.test( txt))
          alert ('Matching');
          }[/code]

          This isn't working.

          Comment

          • mrhoo
            Contributor
            • Jun 2006
            • 428

            #6
            To include the literal escape in a string
            you have to escape the escape- "\\["

            var regStart = new RegExp("^\\["+start+"\\]", "i")

            Comment

            • hsriat
              Recognized Expert Top Contributor
              • Jan 2008
              • 1653

              #7
              Originally posted by mrhoo
              To include the literal escape in a string
              you have to escape the escape- "\\["

              var regStart = new RegExp("^\\["+start+"\\]", "i")
              Well, thats working, and saved me from lots of escape()s and unescape()s later in the code.
              Thank you :)

              But I could not get the reason for escaping the backslash for second time escaping. When did it require to escape it second time in the process?

              Comment

              • mrhoo
                Contributor
                • Jun 2006
                • 428

                #8
                A regular expression uses the slash to escape special characters-

                var rx=/^\[(\w+)\]/

                but strings in javascript escape characters using the same slash

                alert('\[') returns the '[', because [ is not a special character in a string.

                alert('\\[') returns '\[' because the first slash escaped the second- the slash is a special character in strings.

                You will mostly run into it when you create regular expressions from strings,

                Comment

                • hsriat
                  Recognized Expert Top Contributor
                  • Jan 2008
                  • 1653

                  #9
                  Okey... got it now.

                  Thanks for the explanation :)

                  Regards,
                  Harpreet

                  Comment

                  Working...