Skip to main content
dublove
Legend
July 19, 2025
Answered

It's not allowed to be used like this? Regular expressions are replaced with variables.

  • July 19, 2025
  • 1 reply
  • 275 views
Why is that, it says the findWhat property setting is invalid?
What it should say is that gre doesn't have any data
 
I originally got it right by writing it directly like this:
var res = getGrepSearch("@", app.activeDocument);
 
gre = new RegExp('@');
main(){
 var res = getGrepSearch(gre, app.activeDocument);
}
. . . .. .
function getGrepSearch(fp, s) {
    app.findGrepPreferences = app.changeGrepPreferences = app.findChangeGrepOptions = null;
    app.findChangeGrepOptions.properties = { includeHiddenLayers: true, includeLockedLayersForFind: true, includeLockedStoriesForFind: true, includeMasterPages: true }
    app.findGrepPreferences.findWhat = fp;
    //return app.findGrep()
    return s.findGrep()
}

 

Correct answer m1b

@dublove app.findGrepPrefereces.findWhat expects a String, not a RegExp. The string will be used by Indesign's regular expression matching engine. We use RegExp when we are doing matching *without* app.findGrepPreferences. You can't mix them. So this is correct:

(function () {

    var res = getGrepSearch("@", app.activeDocument);

    alert('Matched ' + res.length + ' results.');

})();

function getGrepSearch(findWhat, searchMe) {
    app.findGrepPreferences = app.changeGrepPreferences = app.findChangeGrepOptions = null;
    app.findChangeGrepOptions.properties = { includeHiddenLayers: true, includeLockedLayersForFind: true, includeLockedStoriesForFind: true, includeMasterPages: true }
    app.findGrepPreferences.findWhat = findWhat;

    if (!searchMe || 'function' !== typeof searchMe.findGrep)
        return app.findGrep();
    else
        return searchMe.findGrep();
};

1 reply

m1b
Community Expert
m1bCommunity ExpertCorrect answer
Community Expert
July 19, 2025

@dublove app.findGrepPrefereces.findWhat expects a String, not a RegExp. The string will be used by Indesign's regular expression matching engine. We use RegExp when we are doing matching *without* app.findGrepPreferences. You can't mix them. So this is correct:

(function () {

    var res = getGrepSearch("@", app.activeDocument);

    alert('Matched ' + res.length + ' results.');

})();

function getGrepSearch(findWhat, searchMe) {
    app.findGrepPreferences = app.changeGrepPreferences = app.findChangeGrepOptions = null;
    app.findChangeGrepOptions.properties = { includeHiddenLayers: true, includeLockedLayersForFind: true, includeLockedStoriesForFind: true, includeMasterPages: true }
    app.findGrepPreferences.findWhat = findWhat;

    if (!searchMe || 'function' !== typeof searchMe.findGrep)
        return app.findGrep();
    else
        return searchMe.findGrep();
};
dublove
dubloveAuthor
Legend
July 19, 2025

Thanks for the reminder.
That'll be easy.
I'll go straight:

var what="@";
var repGre = new RegExp(/@/gi)
 
main(){
var res = getGrepSearch(what, app.activeDocument);
myFrame.contents.replace(repGre, "");
}
 
m1b
Community Expert
Community Expert
July 20, 2025

Hi @dublove no that's not quite right. You are doing the find using Indesign's find grep but then doing the replacement using ExtendScripts RegExp. Better to do it all in Indesign using changeGrep. Something like this:

(function () {

    // find "@" and change to "" in document
    doGrep("@", "", app.activeDocument);

})();

/**
 * Perform a findGrep or changeGrep.
 * @param {String} findWhat - the find grep string.
 * @param {String} [changeTo] - the change grep string (default: nothing).
 * @param {Document|Text|*} [searchMe] - the thing to search (default: all documents);
 * @returns {Array<Text>?}
 */
function doGrep(findWhat, changeTo, searchMe) {
    app.findGrepPreferences = app.changeGrepPreferences = app.findChangeGrepOptions = null;
    app.findChangeGrepOptions.properties = { includeHiddenLayers: true, includeLockedLayersForFind: true, includeLockedStoriesForFind: true, includeMasterPages: true }
    app.findGrepPreferences.findWhat = findWhat;

    if (undefined != changeTo) {
        app.changeGrepPreferences.changeTo = changeTo;
        if (!searchMe || 'function' !== typeof searchMe.changeGrep)
            app.changeGrep();
        else
            searchMe.changeGrep();
    }

    if (!searchMe || 'function' !== typeof searchMe.findGrep)
        return app.findGrep();
    else
        return searchMe.findGrep();

};