Created
August 8, 2022 19:59
-
-
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")
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
// 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