Created
July 4, 2016 10:23
-
-
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"
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
/** | |
* 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\_]+/"); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Awesome! Ty