What is the best way to trim() in javascript

前端 未结 19 1972
眼角桃花
眼角桃花 2020-11-29 06:32

The question says it all; JS doesn\'t seem to have a native trim() method.

19条回答
  •  一个人的身影
    2020-11-29 07:00

    You can use following ...

    function trim(str) {
        try {
            if (str && typeof(str) == 'string') {
                return str.replace(/^\s*|\s*$/g, "");
            } else {
                return '';
            }
        } catch (e) {
            return str;
        }
    }
    

提交回复
热议问题