var.replace is not a function

前端 未结 10 2329
悲&欢浪女
悲&欢浪女 2020-12-04 13:50

I\'m using the below code to try to trim the string in Javascript but am getting the error mentioned in the title:

function trim(str) {
    return str.replac         


        
10条回答
  •  隐瞒了意图╮
    2020-12-04 14:19

    You should probably do some validations before you actually execute your function :

    function trim(str) {
        if(typeof str !== 'string') {
            throw new Error('only string parameter supported!');
        }
    
        return str.replace(/^\s+|\s+$/g,'');
    }
    

提交回复
热议问题