How can I parse a template string into its constant and variable parts?
const parse = (constants, ...variables) => ({ constants, variables })
const john = 'Jonathan'
const resultOne = parse(`Hello, ${john}. Nice name, ${john}.`)
console.log(`The result for ${john} is `, resultOne)
const bill = 'William'
const resultTwo = parse`Goodbye, ${bill}. Cool name, ${bill}.`
console.log(`The result for ${bill} is `, resultTwo)
PreviousHow can I tokenise a string of values into an array?NextHow to find out where your function is being called from
Last updated
Was this helpful?