Created
November 13, 2017 07:38
-
-
Save AaronFlower/938674a70f322f27c70e482dbd2d95b7 to your computer and use it in GitHub Desktop.
a simple mustache js
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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