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, "");
     }
posted @ 2017-12-18 07:00  IT-caijw  阅读(2665)  评论(1编辑  收藏  举报