Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

String.prototype.replaceAll 兼容性问题 #106

Open
cklwblove opened this issue Oct 26, 2022 · 0 comments
Open

String.prototype.replaceAll 兼容性问题 #106

cklwblove opened this issue Oct 26, 2022 · 0 comments

Comments

@cklwblove
Copy link
Owner

image

使用 uni-app 开发小程序的时候,发现的此问题。支付宝小程序的时候会报错,uc webview 不支持此方法。需要使用 polyfill(垫片)解决。

/**
 * 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);

        };
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

1 participant