JSFiddle - React, Tailwind, and code Playground
JavaScript
var replacements = {
"%NAME%": "Mike",
"%AGE%": 0,
"%EVENT%": "20"
},
str = 'My Name is %NAME% and my age is %AGE%, the following %TOKEN% is invalid.';
str = str.replace(/%\w+%/g, function(all) {
return all in replacements ? replacements[all] : all;
});
document.body.innerHTML = str;