js remove string spaces

str is the string to be stripped of spaces:

Remove all spaces:   

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

Remove two leading spaces:   

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

Remove left spaces:

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

Remove right spaces:

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

 

SCRIPT LANGUAGE="JavaScript">    

<!--    

//Source: online collection    

// Trim() , Ltrim() , RTrim()    

String.prototype.Trim = function()    

{    

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

}    

String.prototype.LTrim = function()    

{    

returnthis.replace(/(^\s*)/g, "");    

}    

String.prototype.RTrim = function()    

{    

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

}    

//-->    

</SCRIPT>    

<input type="text" value=" space before and after " id="space">    

<input type="button" value="去前后空格" onclick="javascript:document.getElementById('space').value=document.getElementById('space').value.Trim();document.getElementById('space').select();">    

<input type="button" value="去前空格" onclick="javascript:document.getElementById('space').value=document.getElementById('space').value.LTrim();document.getElementById('space').select();">    

<input type="button" value="去后空格" onclick="javascript:document.getElementById('space').value=document.getElementById('space').value.RTrim();document.getElementById('space').select();">    

<input type="button" value="Restore" onclick="javascript:document.getElementById('space').value=' there are spaces before and after ';">    

<a href="http://www.yaoasnsi.com" target="_blank">访问yaosansi.com</a> 

 

Remove all spaces: 

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

Remove two leading spaces: 

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

Remove spaces (used by TimeSheet)

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

Guess you like

Origin http://10.200.1.11:23101/article/api/json?id=326968668&siteId=291194637