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?