使用正则表达式去除字符串开头和格尾部的多余空格

 var str ='  hello  world    ';
        // 去除开头的空格
        str1 = str.replace(/^s*/g ,'')

        //去除结尾的空格
        str2 = str.replace(/s* $/g ,'')

        console.log(str1)
        console.log(str2)

        str_target =str.replace(/^s*|s* $/g , "")
        console.log(str_target)

  效果图:

原文地址:https://www.cnblogs.com/malong1992/p/13377476.html