js去除空格

str为要去除空格的字符串:

去除所有空格:   
str   =   str.replace(/\s+/g,"");       
去除两头空格:   
str   =   str.replace(/^\s+|\s+$/g,"");

去除左空格:

str=str.replace( /^\s*/, '');

去除右空格:

str=str.replace(/(\s*$)/g, "");

  1. SCRIPT LANGUAGE="JavaScript">   
  2.    
  3.   
  4. "text" value="    前后都是空格     " id="space">   
  5. "button" value="去前后空格"οnclick="javascript:document.getElementById('space').value=document.getElementById('space').value.Trim();document.getElementById('space').select();">   
  6. "button" value="去前空格"οnclick="javascript:document.getElementById('space').value=document.getElementById('space').value.LTrim();document.getElementById('space').select();">   
  7. "button" value="去后空格"οnclick="javascript:document.getElementById('space').value=document.getElementById('space').value.RTrim();document.getElementById('space').select();">   
  8. "button" value="还原" οnclick="javascript:document.getElementById('space').value='      前后都是空格     ';">   
  9. "http://www.yaoasnsi.com" target="_blank">访问yaosansi.com  

去除所有空格: 
str = str.replace(/\s+/g,""); 

去除两头空格: 
str = str.replace(/^\s+|\s+$/g,"");

去除空格(TimeSheet用过)

arg0=arg0.replace(/\s+$|^\s+/g,"");

来自:http://hi.baidu.com/qihuitoday/blog/item/eff2f1fbd22229116c22ebe5.html

你可能感兴趣的