Skip to content

Instantly share code, notes, and snippets.

@asika32764
Last active August 29, 2015 14:02
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save asika32764/3a135d47fe39676a7b43 to your computer and use it in GitHub Desktop.
Save asika32764/3a135d47fe39676a7b43 to your computer and use it in GitHub Desktop.
If we use `parseVariable('Hi {{name}}~~~', {name: Simon})`, this function will return `Hi Simon~~~`.
/**
* Parse variable {{name}}.
*
* Example: If we use `parseVariable('Hi {{name}}~~~', {name: 'Simon'})`, this function will return `Hi Simon~~~`.
*
* @param {string} string
* @param {Object} data
*
* @returns {string}
*/
parseVariable: function(string, data)
{
string = string.replace(/\{\{\s*(.+?)\s*\}\}/gm, function(match, contents, offset, s)
{
return data[contents];
});
return string;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment