Find and replace PHP-like parameters for easy string interpolation in javascript

Posted on October 26, 2021

If you want to interpolate a user-provided string that contains some parameters, here my solution using RegExp:

const str = '<h1>$title</h1><p>$text</p><p>$other</p>'
const reg = /\$([a-zA-Z0-9]+)\b/gim
const res = str.match(reg)

const params = {
  $title: 'Some Title',
  $text: 'Some text'
}

let ret = str
res.forEach(p => {
    const v = params[p] || ''
    ret = ret.replace(p, v)
})

console.log(ret)