InDesign's JS interface does not do that for you, it only returns the complete match.
Since contents
is a simple Javascript string (not native InDesign text anymore), you can use Javascript's own match
function:
..
app.findGrepPreferences.findWhat = "(\\d)+(\\d)";
found = app.activeDocument.findGrep();
m = found[0].contents.match (/(\d)+(\d)/);
alert (m.join('\n'));
Be careful not to mix InDesign's GREP syntax with Javascript's. In particular, special characters such as ~<
are ID extensions and will fail in JS.
Note that for an input of "2014" this will return
2014
1
4
where the first line is the full match (equal to $0
), 1
is $1
and 4
is $2
. This is most likely not what you expected. Since you are repeatedly matching "group 1" with the +
, each next single digit replaces the last found one (expect for the very last one). You probably meant something like
(\d+)(\d)
which will return
2014
201
4