Created
September 22, 2017 13:16
-
-
Save batogov/798d2f96a1ead563bd8af2cab149c4c9 to your computer and use it in GitHub Desktop.
Process Template
This file contains 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
function processTemp(tempString, dataObject) { | |
for(var key in dataObject) { | |
if (dataObject.hasOwnProperty(key)) { | |
tempString = tempString.replace(new RegExp('{' + key + '}', 'g'), dataObject[key]); | |
} | |
} | |
return tempString; | |
} | |
var str = 'Hello {name}'; | |
var data = { | |
name: 'Alex' | |
}; | |
var result = processTemp(str, data); | |
console.log(result); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment