Skip to content

Instantly share code, notes, and snippets.

@monkpit
Created August 9, 2021 17:53
Show Gist options
  • Save monkpit/d4d587f5266a9dfde48e91bab47107b5 to your computer and use it in GitHub Desktop.
Save monkpit/d4d587f5266a9dfde48e91bab47107b5 to your computer and use it in GitHub Desktop.
replaceMultiple - replace n pairs of regex/replace in an input string
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