方法如下: function trim(str) { // 匹配开头空格 匹配结尾空格 return str.replace(/(^\s*)|(\s*$)/g, ""); } 如有表述不准确之处,欢迎指正,欢迎补充,感谢阅读。 相关文章: