gwl3323405
JavaScript常用函数之字符串去空格
>>
完整版
显然还有很多人在用JavaScript而非Jquery等框架,而且JavaScript还有有很多无法替代的强大优点。
JavaScript没有trim()函数,想去除字符串的空格却找不到合适办法,这不,有人帮我们实现了(代码摘抄自网络来源已无从考证)!更加强大的去除左右边空格函数巨实用。
[mw_shl_code=javascript,true]//去除左边的空格
function Ltrim(){
document.getElementById("txt").value=(document.getElementById("txt").value).replace( /^\s*/,"");
}
//去除右边的空格
function Rtrim(){
document.getElementById("txt").value=(document.getElementById("txt").value).replace(/(\s*$)/g,"");
}
//去除左右两边的空格
function LRtrim(){
document.getElementById("txt").value=(document.getElementById("txt").value).replace(/^\s+|\s+$/g,"")
}
//去除所有的空格
function trim(){
document.getElementById("txt").value=(document.getElementById("txt").value).replace(/\s+/g,"");
}[/mw_shl_code]