3 Replies Latest reply on Feb 4, 2010 2:04 AM by Peter Kahrel

    [JS CS3] RegExp Object containing only one backslash (how to escape)

    Stefan Huwiler

      Hi

       

      I'm trying to escape one backslash in my RegExp Object, but I get a Syntax Error:

       

           myObj = new RegExp ( "\\" );

       

      I tried it phyton Style   myObj = new RegExp ( "\\\\" ); but that's not working right.

       

      Thanks for any hint.

       

      Stefan

        • 1. Re: [JS CS3] RegExp Object containing only one backslash (how to escape)
          Peter Kahrel Adobe Community Professional & MVP

          myObj = new RegExp ( "\\\\" );

           

          works fine for me. In what way does it not work? Can you give a slightly larger fragment?

           

          Peter

          • 2. Re: [JS CS3] RegExp Object containing only one backslash (how to escape)
            Stefan Huwiler Level 1

            Hi Peter



            Here's an example (Usually the Text would come from outside (e.g Document Text or external Text File):

             

             

            #target InDesign

            main();

            exit();

             

            function myReplaceExample( myText, myWhatSearch, myWhat, myWhit)

            {

                 if ( myText.search( myWhatSearch ) )

                 {

                      myText = myText.replace( myWhat, myWhit );

                 }

                 return myText;

            }

             

            // Main

            function main()

            {

                 var myText = "This is an \\ of search and replace.";

                 var myWhat = "\\";

                 var myWhatSearch = new RegExp ( "\\\\" );

                 var myWhit = new RegExp ( "example" );

                 alert( myReplaceExample( myText, myWhatSearch, myWhat, myWhit) );

                 return true;

            }

             

             


            The result is his is an /example/ of search and replace.

            Stefan


            • 3. Re: [JS CS3] RegExp Object containing only one backslash (how to escape)
              Peter Kahrel Adobe Community Professional & MVP

              Stefan,

               

              First of all, in your sample text you need to escape your backslash as well:

               

              myText = "This is an \\ of search and replace.";

               

              You can see this in these examples:

               

              alert ("a\rb") prints "a" then "b" on a new line; alert ("a\\rb") prints "a\rb".

               

              Another problem is that you use a regex as the replacement object (myWhit), but that should be a string. With these two things corrected, it works fine:

               


              function main()
              {
                   var myText = "This is an \\ of search and replace.";
                   var myWhat = new RegExp ( "\\\\" );
                   var myWhatSearch = new RegExp ( "\\\\" );
                   var myWhit = "example";
                   alert( myReplaceExample( myText, myWhatSearch, myWhat, myWhit) );
                   return true;
              }

               


              That new RegExp ("\\") can't be correct can be seen if you start with a literal regex, in which you would use an escaped backslash:

               

              myText = myText.replace (/\\/, "example")

               

              To use \\ in a new RegExp () construction, you need to place it in a string, and you need to escape both backslashes, so you end up with "\\\\".

               

              Peter