0

I'm trying to do some fancy string manipulation in ExtendScript using Regular Expressions. I'm testing some code that effectively converts single quotes and double quotes to the "curly quotes" or "smart quotes". I use code from here and here as the basis for this function.

sanitize : function (arg) {

   if (arg && (typeof arg == 'string')) {

     arg = arg.replace(/(^|[-\u2014\s(\["])'/g, "$1\u2018");       // opening singles
     arg = arg.replace(/'/g, "\u2019");                            // closing singles & apostrophes
     arg = arg.replace(/(^|[-\u2014/\[(\u2018\s])"/g, "$1\u201c"); // opening doubles
     arg = arg.replace(/"/g, "\u201d");                            // closing doubles
     arg = arg.replace(/--/g, "\u2014");                           // em-dashes

  } 

  return arg;

}

```

Apparently this works in the browser, but does not work within an ExtendScript script. ExtendScript is a superset of Javascript conforming to ECMA-262 Version 3, and I think that the implementation of regular expressions is somewhat different in that version than in modern versions of ECMA script. In any case, does anybody know why I might be getting a "Syntax Error" when I run this code from Adobe's ExtendScript toolkit?

Specifically, the line that is reported as having a Syntax Error is the following one:

arg = arg.replace(/(^|[-\u2014/\[(\u2018\s])"/g, "$1\u201c");

If I remove that line then no Syntax Error is reported, but then the string is not manipulated correctly. I'm at a loss as to how to convert the RegEx to something that can be interpreted by ExtendScript. Any ideas?

ariestav
  • 2,799
  • 4
  • 28
  • 56
  • Maybe that's because of the unescaped slash mark? Escape it and test. – revo Jul 22 '18 at 05:03
  • Which specific slash mark are you referring to? – ariestav Jul 22 '18 at 11:54
  • There is one slash character in your regex. – revo Jul 22 '18 at 11:56
  • I apologize, but I see more than one in the problematic line. Can you be specific? – ariestav Jul 22 '18 at 11:58
  • The one here `\u2014/` – revo Jul 22 '18 at 11:58
  • @revo you were correct! By escaping that forward-slash no Syntax Error is reported. However, it seems like that specific RegEx doesn't do anything to the string. That is, it doesn't replace the "opening doubles" to the curly / smart opening double quotes. I'm not sure what feature of RegEx that the line is using, but maybe it's not supported for ECMA-262? I've heard that some don't have "look-behind" or something... – ariestav Jul 22 '18 at 12:29
  • Lookbehinds are not supported yet (vastly). I think the problem lies behind the logic itself. – revo Jul 22 '18 at 12:37
  • And yet the code itself is duplicated from two different resources. – ariestav Jul 22 '18 at 12:38
  • That's the point. That seconds my word. – revo Jul 22 '18 at 12:46

0 Answers0