javascript的trim方法

﹤scriptlanguage=”javascript”﹥  
/** 
*删除左右两端的空格 
*/ 
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,'');  
}  
﹤/script﹥ 

使用如下:
﹤scripttype=”text/javascript”﹥  
  alert(document.getElementById(’abc’).value.trim());  
  alert(document.getElementById(’abc’).value.ltrim());  
  alert(document.getElementById(’abc’).value.rtrim());  
﹤/script﹥ 

你可能感兴趣的:(JavaScript,prototype)