const regex = /^((?:^|[^\r\n\{]+)(?:\{(?:[^{]|$)|(?:\{{2}model\.variable(?:\|(?:(ein)|(phone)|(zipcode)|(currency:'':0)))?\}{2}|$)))+$/gm;
// Alternative syntax using RegExp constructor
// const regex = new RegExp('^((?:^|[^\\r\\n\\{]+)(?:\\{(?:[^{]|$)|(?:\\{{2}model\\.variable(?:\\|(?:(ein)|(phone)|(zipcode)|(currency:\'\':0)))?\\}{2}|$)))+$', 'gm')
const str = `Any text, punctuation (except double-{), or numb3r5 {{model.variable|phone}} is valid
Any text, punctuation (except double-{), or numb3r5 {{model.variable}} is valid.
Something about {{model.variable|phone}} or {{model.variable|zipcode}}
{{model.variable|zipcode}}
Something about {{model.variable|phone}} or {{model.variable|fax}} - shouldn't work, fax isn't an option
Any deviation from the format, e.g. {{model.variable|custom}} makes the entire input invalid.
(?=model\\.variable(?:\\|(?:accepted|variable|names|phone)`;
const subst = `$1$2$3 -- `;
// 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