[JS] Problem with change grep with look behind/ahead and /K
- March 16, 2022
- 6 replies
- 1355 views
Dear forum,
I encountered this problem while writing a complex script that imitates the find-change dialog. Its purpose is to find RegExes at the beginning and/or end of lines (which InDesign doesn’t allow).
Let’s illustrate my issue with an example. (I attached a test document below).
I simplified things to make my question as clear as possible.
First, I set find what to ra\> (ra at the end of a word) and change format to, say, blue color.

And run this script:
main();
function main() {
var txt, changed,
doc = app.activeDocument,
found = doc.findGrep(false);
for (var i = 0; i < found.length; i++) {
txt = found[i];
changed = txt.changeGrep();
$.writeln( i + " - " + changed.length);
}
}It works as expected: all 6 found items are changed one by one:

Now let’s change find what to a regex with look behind: (?<=illo)ra

And run the script again. As before, 6 items were found, but were not changed:

When I change it manually in InDesign one by one, it works.
The same happens with regexes using look ahead and /K, for example:
- illo(?=ra)
- illo\Kra
Any ideas why this happens?

