Created
August 9, 2021 17:53
-
-
Save monkpit/d4d587f5266a9dfde48e91bab47107b5 to your computer and use it in GitHub Desktop.
replaceMultiple - replace n pairs of regex/replace in an input string
This file contains hidden or 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
const replaceMultiple = (input, ...regexReplacePairs) => | |
regexReplacePairs.reduce((previous, [ regex, replace ] ) => | |
previous.replace(regex, replace), input | |
); | |
const template = '<header> \n 12345 \n <body> \n --- \n <footer>'; | |
const header = 'Welcome to my blog!'; | |
const body = 'This is the body of my post.'; | |
const footer = 'All rights reserved.'; | |
const result = replaceMultiple( | |
template, | |
[/<header>/g, header], | |
[/<body>/g, body], | |
[/<footer>/g, footer] | |
); | |
console.log(result); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment