• Global community
    • Language:
      • Deutsch
      • English
      • Español
      • Français
      • Português
  • 日本語コミュニティ
    Dedicated community for Japanese speakers
  • 한국 커뮤니티
    Dedicated community for Korean speakers
Exit
0

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

Engaged ,
Mar 13, 2022 Mar 13, 2022

Copy link to clipboard

Copied

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.

Views

499

Translate

Translate

Report

Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Community Expert ,
Aug 19, 2022 Aug 19, 2022

Copy link to clipboard

Copied

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

  • Neiter with the standard FM Find/Change dialogue
  • RegEx-1.png
  • Nor with my own FMfindReplace scipt.
  • RegEx-2.png
  • 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 ​

Votes

Translate

Translate

Report

Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Community Expert ,
Jul 05, 2023 Jul 05, 2023

Copy link to clipboard

Copied

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

Votes

Translate

Translate

Report

Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Engaged ,
Jul 06, 2023 Jul 06, 2023

Copy link to clipboard

Copied

LATEST

Votes

Translate

Translate

Report

Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines