首页 > Web开发 > 详细

js中去除字符串两边的空格

时间:2018-01-11 12:48:55      阅读:193      评论:0      收藏:0      [点我收藏+]

在提交表单的时候会需要去除字符串两边的空格,代码如下:

/*去除字符串两边空格*/
String.prototype.trim = function() {
    return this.replace(/^\s*|\s*$/g, "");
}
/*去除字符串左边空格*/
String.prototype.ltrim = function(){
    return this.replace(/^\s*/g,"")
}
/*去除字符串右边空格*/
String.prototype.rtrim = function(){
    return this.replace(/\s*$/g,"")
}

例:str = "   fsfa  fasdf  sfas  ";

  str.trim() == "fsfa  fasdf  sfas";

js中去除字符串两边的空格

原文:https://www.cnblogs.com/suhfj-825/p/8267253.html

(0)
(0)
   
举报
评论 一句话评论(0
关于我们 - 联系我们 - 留言反馈 - 联系我们:wmxa8@hotmail.com
© 2014 bubuko.com 版权所有
打开技术之扣,分享程序人生!