Skip to content

Instantly share code, notes, and snippets.

@HynesIP
Forked from Paradoxis/findReplace.js
Last active June 16, 2019 15:39
Show Gist options
  • Save HynesIP/625daab4b120ac7ff286bec4b2ae292b to your computer and use it in GitHub Desktop.
Save HynesIP/625daab4b120ac7ff286bec4b2ae292b to your computer and use it in GitHub Desktop.
Find and replace double curly braces in JavaScript, example: findReplace("Hello, {{ name }}", "name", "John"); // "Hello, John"
/**
* Format double braced template string
* @param {string} string
* @param {string} find
* @param {string} replace
* @returns {string}
*/
function Maparameter(string, find, replace)
{
if ((/[a-zA-Z\_]+/g).test(string)) {
return string.replace(new RegExp('\{\{(?:\\s+)?(' + find + ')(?:\\s+)?\}\}'), replace);
} else {
throw new Error("Find statement does not match regular expression: /[a-zA-Z\_]+/");
}
}
Maparameter("{{CustomerGuid}}", "CustomerGuid", "1234-4567-7891-1234");
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment