把它写在你的开头处
String.prototype.trim = function(){
return this.replace(/(^\s*)|(\s*$)/g, "");
}
就可以调用String.trim(str)方法了
或者这样
function trim(s) {
return s.replace(/(^\s*)|(\s*$)/g,"");
}
String.prototype.trim = function(){
return this.replace(/(^\s*)|(\s*$)/g, "");
}
就可以调用String.trim(str)方法了
或者这样
function trim(s) {
return s.replace(/(^\s*)|(\s*$)/g,"");
}
function BASEtrim(str){
str=str.toString()
lIdx=0;rIdx=str.length;
if (BASEtrim.arguments.length==2)
act=BASEtrim.arguments[1].toLowerCase()
else
act="all"
for(var i=0;i<str.length;i++){
thelStr=str.substring(lIdx,lIdx+1)
therStr=str.substring(rIdx,rIdx-1)
if ((act=="all" || act=="left") && thelStr==" "){
lIdx++
}
if ((act=="all" || act=="right") && therStr==" "){
rIdx--
}
}
str=str.slice(lIdx,rIdx)
return str
用法:
BASEtrim(str{,[left]/[right]/{all}}
例:
BASEtrim(text1.value,left)
BASEtrim(text1.value,all)
BASEtrim(text1.value)