var.replace is not a function

前端 未结 10 2311
悲&欢浪女
悲&欢浪女 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:32

    Replace wouldn't replace numbers. It replaces strings only.

    This should work.

    function trim(str) {
        return str.toString().replace(/^\s+|\s+$/g,'');
    }
    

    If you only want to trim the string. You can simply use "str.trim()"

提交回复
热议问题