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

JS去除字符串前后空格的几种方法

时间:2019-05-15 16:00:45      阅读:133      评论:0      收藏:0      [点我收藏+]

标签:代码   空格   prot   prototype   字符串替换   function   test   正则表达   ace   

test

    // 1,使用原生的字符串trim方法
     var str = "   ss  ";
    str.trim();
    // 2 使用正则表达式将空白字符串替换成空字符串
    str.replace (/^\s*|\s*$/g,");
    // 如果浏览器不支持trim方法,可在代码前加上
    if(!String.prototype.trim) {
        String.prototype.trim = function () {
            reuren this.replace(/^\s*|\s*$/g,"");
}
} 

JS去除字符串前后空格的几种方法

标签:代码   空格   prot   prototype   字符串替换   function   test   正则表达   ace   

原文地址:https://www.cnblogs.com/longtao/p/10869814.html

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