summaryrefslogtreecommitdiff
path: root/qutebrowser/javascript/quirks/string_replaceall.user.js
blob: 03e079364159c26597b29686802b42293ddb3a0f (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
/* eslint-disable no-extend-native,no-implicit-globals */

"use strict";

// https://developer.mozilla.org/en-US/docs/Web/JavaScript/Guide/Regular_Expressions
function escapeRegExp(string) {
    return string.replace(/[.*+?^${}()|[\]\\]/g, "\\$&");
}

// Based on: https://vanillajstoolkit.com/polyfills/stringreplaceall/
/**
 * 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) === "[object RegExp]") {
            return this.replace(str, newStr);
        }

        // If a string
        return this.replace(new RegExp(escapeRegExp(str), "g"), newStr);
    };
}