js控制去掉字符串头尾空格

JS去掉首尾空格 简单方法大全(原生正则jquery)

var str= '       http://www.baidu.com/  ';
 
//去除首尾空格
str.replace(/(^s*)|(s*$)/g, "");
 
//去除左边空格
str.replace(/(^s*)/g, "");
 
//去除右边空格
str.replace(/(s*$)/g, "");
 
//jquery 需提前引用jquery
$.trim(str);
//另一种写法
jQuery.trim(str);

 String.prototype.Trim = function(){
     	return this.replace(/(^s*)|(s*$)/g, "");
     }
原文地址:https://www.cnblogs.com/caijw/p/8055896.html