Would you like to clone this notebook?

When you clone a notebook you are able to make changes without affecting the original notebook.

Cancel

fallback finder

node v14.20.1
version: 4.0.0
endpointsharetweet
function findDefaultMerges(content, mergeFields) { const startPattern = '\\{\\{\\s*' const endPattern = '\\s*\\|\\s*([^\}\{\|]+)\\s*\\}\\}' const defaultValues = mergeFields.reduce((acc, key) => { const fallbackFinder = new RegExp(startPattern + '(' + key + ')' + endPattern) const matches = fallbackFinder.exec(sampleText); if (matches && matches.length >= 3) { acc[key] = matches[2].trim() content = content.replace(fallbackFinder, `\{\{ ${key} \}\}`) } return acc }, {}) return { defaultValues, content } }
const mergeFields = ['first_name', 'last_4', 'card_brand'] const sampleText = `Hello {{ first_name | multi-word fall back }} card {{ last_4 | 23094 }} and more.`
findDefaultMerges(sampleText, mergeFields)
Loading…

no comments

    sign in to comment