3 Replies Latest reply on Jan 26, 2016 1:09 PM by Schmaltzkopf

    Why does GREP expression need two back-slashes?

    Schmaltzkopf Level 1

      Hi, there.

      Curious question:

      I have the following code:



              appliedCharacterStyle: csYSimporedPsukimMekor,

              grepExpression: "\\(.+?\\)"});


      Why does this only work if I have double-backslashes before "()" and "[]"?

      If I only have a single backslash it will totally ignore the backslash.

      Anyone could explain this quirk?

        • 1. Re: Why does GREP expression need two back-slashes?
          Trevorׅ Adobe Community Professional



          The "/" character is an escape character. For example if you want to alert "the "big" cat" with the word cat in quotes one way of doing it is alert("the \"big\" cat");

          Because you are providing a string you need to escape the escape

          Try the following 2 examples

          alert("\(") // => (
          alert("\\(") // => \(

          What you want is the second example as the "correct" grep is \( and not (


          Got it?



          • 2. Re: Why does GREP expression need two back-slashes?
            Marc Autret Level 4



            Another way to explain the double-escape trick is to consider those two distinct levels:


            1. At the GREP (or regular expression) level, parentheses ( and ) are special symbols that control capturing groups.

            So if you want to capture a left parenthesis as itself you need to use an escape sequence.

            In GREP, the escape character is \ (backslash) so that the full escape sequence is \( (backslash + parenthesis.)


            2. At the JavaScript level (which is the language you are using to send the above GREP command), the backslash \ is a special symbol when involved in a literal string. Indeed, that symbol is used to form escape sequences!

            So if you want to provide the backslash as itself in a literal string, you need the following escape sequence, "\\" (backslash + backslash), which in fact represents a single backslash character.


            Finally, in order to encode in JavaScript the full GREP pattern \( —that is, a backslash and a left parenthesis—you'll have to use the literal string "\\(".


            [The reasoning is the same for square brackets.]


            Hope that helps.




            • 3. Re: Why does GREP expression need two back-slashes?
              Schmaltzkopf Level 1

              Thank you Trevor.

              I'm used to the UI GREP codes, so I appreciate your short and sweet explanation that JS strings are different