Skip to content

Instantly share code, notes, and snippets.

@AaronFlower
Created November 13, 2017 07:38
Show Gist options
  • Save AaronFlower/938674a70f322f27c70e482dbd2d95b7 to your computer and use it in GitHub Desktop.
Save AaronFlower/938674a70f322f27c70e482dbd2d95b7 to your computer and use it in GitHub Desktop.
a simple mustache js
str = 'hello {name}, say {msg}. {name} is a handsome boy, {age}!'
reg = /{([^{}]+)}/g
data = {
name: 'eason',
msg: 'hi'
}
str.replace(reg, function (_, key) {
return data[key] || `{${key}}`
})
// "hello eason, say hi. eason is a handsome boy, {age}!"
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment