Skip to main content
KlausKi
Inspiring
March 13, 2022
Question

Regular Expression bug: characters in a positive character group must redundantly be escaped

  • March 13, 2022
  • 2 replies
  • 663 views

I've been struggling with the Regular Expression implementation of ExtendScript quite a bit, because it doesn't parse Regular Expressions correctly.

 

The following information may be helpful to others:

 

The ExtendScript Regular Expression parser fails to correctly parse positive character groups (characters enclosed in square brackets). Quantifiers and the dot (".") don't make sense in a positive character group, so characters representing these quantifiers are not escaped in positive character groups. However, for the ExtendScript Regular Expression parser to interpret positive character groups, you MUST escape those characters.

 

For example:

 

A valid Regular Expression like this:

 

/[?.*]/

 

… must be written like this in ExtendScript:

 

/[\?\.\*]/

 

… for the ExtendScript Regular Expression parser to accept the Regular Expression.

    This topic has been closed for replies.

    2 replies

    Community Expert
    July 6, 2023

    Hi @KlausKi,

    Which application are you targetting your code to. I have never faced this issue, though mainly I have worked with InDesign and a bit with Illustrator and PS. I just tried the following code on InDesign 2023 and it worked for me

    var a = "hello? Nwo."
    var r = a.search(/[?*.]/)
    alert(r)

    -Manan

    -Manan
    KlausKi
    KlausKiAuthor
    Inspiring
    July 6, 2023
    K.Daube
    Community Expert
    Community Expert
    August 19, 2022

    MHM, I don't get this problem in FM-15 (2019)

    • Neiter with the standard FM Find/Change dialogue
    • Nor with my own FMfindReplace scipt.
    • FMfindRepl uses this code for replacement if something was found by the ES Find method:
      KLD_F.ReplaceInString = function (string, sSearch, sReplace, bCase, bWord, bRegEx) {
      /*            Find substring with options and replae it
      Arguments     string          String in which to search
                    sSearch         String to be searched
                    sReplace        String replacing the search
                    bCase           true for case sentitive search
                    bWord           true so search whole words only
                    bRegEx          true to interprete sSearch as RegEx
      Returns       The string with the replacement
      Called by     ButtonReplace
      Calling       -
      History       2021-10-18
      */
      var eReg0, eReg1, eReg2, eReg2, eReg3, eRegR, sResult, sFind;
      
        if (string === undefined) { string = ""; }
        if (!bRegEx) { // escape the 12 syntactical chars
          sFind = sSearch.replace (/([\\\^\$\.\|\?\*\+\(\)\[\{])/g,"\\$1"); 
        }
      
        eReg0 = new RegExp (sFind, "i");          // case insesitive, no words
        eReg1 = new RegExp (sFind);               // case sensitive, no words
        eReg2 = new RegExp (sFind + "\\b", "i");  // case insesitive, word
        eReg3 = new RegExp (sFind + "\\b");       // case sensitive, word
      
        if (bRegEx) {
          eRegR = new RegExp (sSearch);   // interprete as RegEx
          sResult = string.replace (eRegR, sReplace);
        } else {
          if (!bCase && !bWord) {
            sResult = string.replace (eReg0, sReplace);
          } else if (bCase && !bWord) {
            sResult = string.replace (eReg1, sReplace);
          } else if (!bCase && bWord) {
            sResult = string.replace (eReg2, sReplace);
          } else {
            sResult = string.replace (eReg3, sReplace);
          }
        }
        return sResult;
      } //--- end ReplaceInString ​