码迷,mamicode.com
首页 > Web开发 > 详细

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

时间:2018-01-11 16:02:55      阅读:176      评论:0      收藏:0      [点我收藏+]

标签:prototype   字符   ret   style   去除   ace   col   str   string   

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

/*去除字符串两边空格*/
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中去除字符串两边的空格

标签:prototype   字符   ret   style   去除   ace   col   str   string   

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

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!