js 去除字符串中的空格

    name = $.trim(name); jquery去除字符串左右空格
    name = name.replace(/\s+/g,"");  js 正则去除所有空格
    name = name.replace(/(^\s*)/,""); js 正则去除左侧空格
    name = name.replace(/(\s*$)/g,""); js 正则去除有侧空格
    name = name.replace(/(^\s*)|(\s*$)/g,""); js正则去除左右两侧的空格

下面是在网上找的
参考连接

SCRIPT LANGUAGE="JavaScript">    
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="    前后都是空格     " 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="还原" onclick="javascript:document.getElementById('space').value='      前后都是空格     ';">    
<a href="http://www.yaoasnsi.com" target="_blank">访问yaosansi.com</a> 

参考连接

3、现在大部分浏览器中基本上都支持字符串的 trim 函数,但是为了兼容不支持的浏览器,我们最好还是在 Js 文件中加入以下代码(不需要清除换行符的请删除 \n 制表符删除 \t):

if (!String.prototype.trim) {

     /*---------------------------------------
      * 清除字符串两端空格,包含换行符、制表符
      *---------------------------------------*/
     String.prototype.trim = function () { 
      return this.triml().trimr(); 
 }

 /*----------------------------------------
  * 清除字符串左侧空格,包含换行符、制表符
  * ---------------------------------------*/
 String.prototype.triml = function () {
      return this.replace(/^[\s\n\t]+/g, "");
 }

 /*----------------------------------------
  * 清除字符串右侧空格,包含换行符、制表符
  *----------------------------------------*/
 String.prototype.trimr = function () {
      return this.replace(/[\s\n\t]+$/g, "");
 }
}
//如果只需要 trim 函数的,可以只写一个:
if (!String.prototype.trim){

    /*---------------------------------------
      * 清除字符串两端空格,包含换行符、制表符
      *---------------------------------------*/
     String.prototype.trim = function () { 
      return this.replace(/(^[\s\n\t]+|[\s\n\t]+$)/g, "");
 }

}

https://blog.csdn.net/huanghui8030/article/details/14517525 这个非常厉害

String.prototype.trim = function() {
  var str = this,
  whitespace = ' \n\r\t\f\x0b\xa0\u2000\u2001\u2002\u2003\u2004\u2005\u2006\u2007\u2008\u2009\u200a\u200b\u2028\u2029\u3000';
  for (var i = 0,len = str.length; i < len; i++) {
    if (whitespace.indexOf(str.charAt(i)) === -1) {
      str = str.substring(i);
      break;
    }
  }
  for (i = str.length - 1; i >= 0; i--) {
    if (whitespace.indexOf(str.charAt(i)) === -1) {
      str = str.substring(0, i + 1);
      break;
    }
  }
  return whitespace.indexOf(str.charAt(0)) === -1 ? str : '';
}

猜你喜欢

转载自blog.csdn.net/liguangix/article/details/80262702