@_AWID_ you are on the right track with "doing something" with regular expressions—they are definitely going to do some heavy lifting here!
I've written a script that should do what you describe, and hopefully it will help you with your own scripting.
- Mark
/**
* @file Fix Index Lines.js
*
* @author m1b
* @version 2025-10-08
* @discussion https://community.adobe.com/t5/indesign-discussions/adjusting-rows-and-tabs-in-an-index-using-a-script/m-p/15538412
*/
function main() {
var doc = app.activeDocument;
var text = doc.selection[0];
if ('function' !== typeof text.findGrep)
return alert('Please select some text and try again.');
app.findGrepPreferences = NothingEnum.NOTHING;
app.findGrepPreferences.findWhat = '~y\\K[\\d, -]+';
var found = text.findGrep();
var delim = /([\s,]+)/g;
for (var i = found.length - 1, refText, refs, half, pos; i >= 0; i--) {
refText = found[i];
if (refText.lines.length < 2)
// already one line
continue;
half = Math.ceil(refText.contents.length / 2);
refs = refText.contents.split(delim);
pos = 0;
// find the position at or after the halfway point
for (var j = 1; j < refs.length && pos < half; j = j + 2)
pos += refs[j - 1].length + refs[j].length;
refText.insertionPoints.item(pos).contents = SpecialCharacters.RIGHT_INDENT_TAB;
refText.insertionPoints.item(pos).contents = SpecialCharacters.FORCED_LINE_BREAK;
}
};
app.doScript(main, ScriptLanguage.JAVASCRIPT, undefined, UndoModes.ENTIRE_SCRIPT, 'Fix Index Lines');
Notes:
(a) I'm searching using Indesign's native findGrep, using this grep, which you may have to adjust to suit your document:
~y\K[\d, -]+
which means
~y = match a right indent tab
\K = reset (ignore what we matched
so far and keep going)
[\d, -]+ = match one or more of any combo
of digit, comma, space or hyphen
* oh, and to make things a little more complex, because Text.findGrep() takes a String, not a RegExp, we need to escape the backslashes (with a backslash!) so that's why you are seeing double!
(b) I'm using a RegExp with a capture group as the String.split parameter, which means that the resulting array will include the matched delimiter text as separate elements in the array, so a string split into 3 pieces will actually have 5 elements. That's why I jump by 2 in the "j" loop.