From 7786044d98e7ac1f6140897dbbbae741711df67a Mon Sep 17 00:00:00 2001 From: minjaesong Date: Tue, 1 Dec 2020 23:27:15 +0900 Subject: [PATCH] String.prototype.replaceAll polyfilling --- assets/JS_INIT.js | 19 +++++++++++++++++++ 1 file changed, 19 insertions(+) diff --git a/assets/JS_INIT.js b/assets/JS_INIT.js index a11bc30..e197834 100644 --- a/assets/JS_INIT.js +++ b/assets/JS_INIT.js @@ -56,6 +56,25 @@ if (!String.prototype.endsWith) { } }); } +/** + * String.prototype.replaceAll() polyfill + * https://gomakethings.com/how-to-replace-a-section-of-a-string-with-another-one-with-vanilla-js/ + * @author Chris Ferdinandi + * @license MIT + */ +if (!String.prototype.replaceAll) { + String.prototype.replaceAll = function(str, newStr){ + + // If a regex pattern + if (Object.prototype.toString.call(str).toLowerCase() === '[object regexp]') { + return this.replace(str, newStr); + } + + // If a string + return this.replace(new RegExp(str, 'g'), newStr); + + }; +} if (!Array.prototype.filter){ Array.prototype.filter = function(func, thisArg) { 'use strict';