A tiny change does it.
/**
* Repeat 4 digit codes at start of paragraphs.
* Codes are identified by their character style.
* @7111211 m1b
* @discussion https://community.adobe.com/t5/indesign-discussions/copy-paste-4-digits-at-the-beginning-of-a-paragraph/m-p/13773037
*/
function main() {
var settings = {
characterStyleName: 'red',
afterCode: ' ',
ignoreParagraph: /^\d{4}/,
};
var doc = app.activeDocument;
app.findGrepPreferences = NothingEnum.NOTHING;
app.changeGrepPreferences = NothingEnum.NOTHING;
app.findGrepPreferences.appliedCharacterStyle = settings.characterStyleName;
var found = doc.findGrep();
for (var i = found.length - 1; i >= 0; i--) {
if (settings.ignoreParagraph.test(found[i].paragraphs[0].contents))
continue;
found[i].paragraphs[0].insertionPoints[0].contents = found[i].texts[0].contents + settings.afterCode;
}
}
app.doScript(main, ScriptLanguage.JAVASCRIPT, undefined, UndoModes.ENTIRE_SCRIPT, 'Add Codes To Start');