const regex = /^(.*)(\|.*)(\n(?:(?!msgstr).*\n)*)(msgstr)/gm;
// Alternative syntax using RegExp constructor
// const regex = new RegExp('^(.*)(\\|.*)(\\n(?:(?!msgstr).*\\n)*)(msgstr)', 'gm')
const str = `translate italian ch1_669664cb:| mc
# "Hey."
msgstr "Hey."
translate italian ch1_177d3eb2:| mc
# "What are you doing?"
msgstr "What are you doing?"
translate italian ch1_25d98ea6:| m p_1a
# "I'm in charge of the breakfast, remember?"
msgstr "I'm in charge of the breakfast, remember?"
translate italian ch1_7ae83e04:|
# "Oh, that's right."
msgstr "Oh, that's right."`;
const subst = `$1$4$3$2`;
// The substituted value will be contained in the result variable
const result = str.replace(regex, subst);
console.log('Substitution result: ', result);
Please keep in mind that these code samples are automatically generated and are not guaranteed to work. If you find any syntax errors, feel free to submit a bug report. For a full regex reference for JavaScript, please visit: https://developer.mozilla.org/en/docs/Web/JavaScript/Guide/Regular_Expressions