[转]JavaScript中的trim函数

来源:互联网 发布:微信游戏源码带后台 编辑:程序博客网 时间:2024/04/30 08:58

/**
* 去除多余空格函数
* trim:去除两边空格 lTrim:去除左空格 rTrim: 去除右空格
* 用法:
*     var str = "  hello ";
*     str = str.trim();
*/
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, "");
}

原创粉丝点击