写一个function,清除字符串前后的空格

  • 使用自带接口trim(),考虑兼容性:
if (!String.prototype.trim) {
    String.prototype.trim = function() {
        return this.replace(/^\s+/, "").replace(/\s+$/,"");
    }
}

 // test the function
 var str = " \t\n test string ".trim();
 alert(str == "test string"); // alerts "true"

猜你喜欢

转载自blog.csdn.net/LuckXinXin/article/details/107481433