Skip to content

Instantly share code, notes, and snippets.

@alexmccabe
Last active January 27, 2017 15:14
Show Gist options
  • Save alexmccabe/08fd5e4a53abb60eb82eebd1bf1089eb to your computer and use it in GitHub Desktop.
Save alexmccabe/08fd5e4a53abb60eb82eebd1bf1089eb to your computer and use it in GitHub Desktop.
Converts a string containing Moustache style curly braces into a proper string.
(function () {
'use strict';
var textReplacement = {
/**
* Converts a string containing Moustache style curly-brace variables,
* into a coherent string.
* @param {string} string Input string containing
* {{ curlyBrace }} variables
* @param {object} replacements key / value pair object where the key
* is the variable in the string, and the
* value is the replacement string.
* @return {string}
*/
replace: function (string, replacements) {
var key;
var regex;
for (key in replacements) {
if (replacements.hasOwnProperty(key)) {
regex = new RegExp('\\{\\{([\\s?' + key + '\\s?]*?)\\}\\}', 'g');
string = string.replace(regex, replacements[key]);
}
}
key = null;
regex = null;
return string;
},
};
module.exports = textReplacement;
}());
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment