Replace multiple characters in one replace call

前端 未结 15 2585
失恋的感觉
失恋的感觉 2020-11-22 17:24

Very simple little question, but I don\'t quite understand how to do it.

I need to replace every instance of \'_\' with a space, and every instance of \'#\' with no

15条回答
  •  天命终不由人
    2020-11-22 17:53

    Here is another version using String Prototype. Enjoy!

    String.prototype.replaceAll = function(obj) {
        let finalString = '';
        let word = this;
        for (let each of word){
            for (const o in obj){
                const value = obj[o];
                if (each == o){
                    each = value;
                }
            }
            finalString += each;
        }
    
        return finalString;
    };
    
    'abc'.replaceAll({'a':'x', 'b':'y'}); //"xyc"
    

提交回复
热议问题