JS替换所有功能:错误 [英] JS Replace All Function : Error

查看:125
本文介绍了JS替换所有功能:错误的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

这是我的代码:



This is my code :

String.prototype.replaceAll = function(search, replace){
    if(!replace){
        return this;
    }
    
    while(this.indexOf(search) !== false){
        this.replace(search, replace);
    }
    
    return this;
};





但是,它不起作用。我该怎么办?



However, it doesn't works. What can I do?

推荐答案

这是有效的:

This is one that works:
function replaceAll(strString, oldValue, newValue) {
    while (strString.indexOf(oldValue) != -1) {
        strString = strString.replace(oldValue, newValue);
    }
    return strString;
}


这篇关于JS替换所有功能:错误的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆