码迷,mamicode.com
首页 > 其他好文 > 详细

去除字符串中空格

时间:2017-07-05 15:14:36      阅读:126      评论:0      收藏:0      [点我收藏+]

标签:remove   str   space   alert   return   pac   rem   去除   字符串   

去除字符串前后的空格

function trim(str) {
return str.replace(/(^\s+)|(\s+$)/g, "");
}

去除字符串中所有空格

function removeAllSpace(str) {
return str.replace(/\s+/g, "");
}

用法举例:
alert(trim(‘ ab cd ‘)); //结果: ab cd
alert(removeAllSpace(‘ ab cd ‘)); //结果: abcd

去除字符串中空格

标签:remove   str   space   alert   return   pac   rem   去除   字符串   

原文地址:http://www.cnblogs.com/yangxiong/p/7121223.html

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