自己编写JS的trim方法

来源:互联网 发布:宜家椅子 知乎 编辑:程序博客网 时间:2024/04/30 02:01

由于JS的trim方法在很多浏览器中不支持 所以在这里我们可以使用自己编写的trim方法来处理一些剪掉字符串两端的空字符串的需要 处理方法有很多种 这里我只选择一种 是经过自己测试的

String.prototype.trim = function () {
                                    return this .replace(/^\s\s*/, '' ).replace(/\s\s*$/, '');
                                 }

原创粉丝点击