Skip to content

Instantly share code, notes, and snippets.

Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save DarrenSem/5b8842d649b2218505675f924f7886a4 to your computer and use it in GitHub Desktop.
Save DarrenSem/5b8842d649b2218505675f924f7886a4 to your computer and use it in GitHub Desktop.
replaced(original, pattern1, replacement1, patternX, replacementX).js - enables MULTIPLE PAIRS in a single call - very flexible syntax e.g. replaced(original, ["pattern(number)1", "replacement$1"], /p2/, "r2", "p3", "r3")
// replaced(original, pattern1, replacement1, patternX, replacementX).js - enables MULTIPLE PAIRS in a single call - very flexible syntax e.g. replaced(original, ["pattern(number)1", "replacement$1"], /p2/, "r2", "p3", "r3")
var replaced = (p, ...pairsFindReplacewith) => pairsFindReplacewith.flat(Infinity).reduce(
(curr, string, i) =>
i % 2 ? curr.replace(p, string)
: (p = RegExp(string, "g"), curr)
, String(p != null ? p : "") // AKA String(p ?? "") // p != null ? String(p) : ""
)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment