Skip to content

Instantly share code, notes, and snippets.

@Paradoxis
Created July 4, 2016 10:23
Show Gist options
  • Save Paradoxis/14997049256dd01c5b36fc11a06fe9cf to your computer and use it in GitHub Desktop.
Save Paradoxis/14997049256dd01c5b36fc11a06fe9cf 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 findReplaceString(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\_]+/");
}
}
@HynesIP
Copy link

HynesIP commented Jun 16, 2019

This is perfect. Thank you.

@caleb87
Copy link

caleb87 commented Mar 10, 2020

Awesome! Ty

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment