js去除空格或所有空格

function trim(str)
{ 

    return str.replace(/(^\s*)|(\s*$)/g, ""); 

}
/**
*
is_global
  设置"g" 去除所有空格,设置其它只去除前后空格。
**/
function trim(str,is_global)
{
    var result;
    result = str.replace(/(^\s+)|(\s+$)/g,"");
    if(is_global.toLowerCase()=="g")
    {
        result = result.replace(/\s/g,"");
    }
    return result;
}

 

posted @ 2019-03-08 16:49  稚语希听  阅读(181)  评论(0编辑  收藏  举报