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.
Copy link to clipboard
Copied
MHM, I don't get this problem in FM-15 (2019)
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
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
Copy link to clipboard
Copied
I've been programming a script for PhotoShop CC 2022:
https://github.com/SetTrend/Raw-Image-Converter/blob/main/includes/_FileOperations.jsx#L111