亚洲乱码中文字幕综合,中国熟女仑乱hd,亚洲精品乱拍国产一区二区三区,一本大道卡一卡二卡三乱码全集资源,又粗又黄又硬又爽的免费视频

javascript去掉前后空格的實(shí)例

 更新時(shí)間:2013年11月07日 10:21:59   投稿:zxhpj  
在js中去除空格并不是可以直接使用trim來刪除,它在js中并不像我們想的那么好用,特別像我要?jiǎng)h除前后空格這個(gè)trim函數(shù)更不可靠了

代碼

復(fù)制代碼 代碼如下:

function String.prototype.Trim() { return this.replace(/(^/s*)|(/s*$)/g, ""); }   // 去掉左右空格
function String.prototype.Ltrim() { return this.replace(/(^/s*)/g, ""); }            // 去掉左空格
function String.prototype.Rtrim() { return this.replace(/(/s*$)/g, ""); }            // 去掉右空格
 


<script type="text/javascript">
   function trim(str){ //刪除左右兩端的空格
     return str.replace(/(^s*)|(s*$)/g, "");
   }
   function ltrim(str){ //刪除左邊的空格
     return str.replace(/(^s*)/g,"");
   }
   function rtrim(str){ //刪除右邊的空格
     return str.replace(/(s*$)/g,"");
   }
</script>

去除所有空格

<SCRIPT LANGUAGE="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>

JS 去字符串空格 總結(jié)

str為要去除空格的字符串:
去除所有空格:
str = str.replace(/\s+/g,"");
去除兩頭空格:
str = str.replace(/^\s+|\s+$/g,"");
去除左空格:
str=str.replace( /^\s*/, '');
去除右空格:
str=str.replace(/(\s*$)/g, "");

<SCRIPT LANGUAGE="JavaScript">  
<!--  
//出處:網(wǎng)上搜集  
// 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="  前后都是空格   " 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='   前后都是空格   ';"> 

復(fù)制代碼 代碼如下:

去除所有空格:
str = str.replace(/\s+/g,"");
去除兩頭空格:
str = str.replace(/^\s+|\s+$/g,"");
去除空格(TimeSheet用過)
arg0=arg0.replace(/\s+$|^\s+/g,"");

下面來我們來看看Js腳本中"/s表示什么"
s 匹配任何空白字符,包括空格、制表符、換頁(yè)符等等。等價(jià)于 [ fnrtv]。
 請(qǐng)緊記是小寫的s,另外細(xì)心的朋友可能看到有時(shí)候\s+有時(shí)候是*號(hào),大家可以看下面的解釋

常用的限定符
代碼/語(yǔ)法 說明
* 重復(fù)零次或更多次
+ 重復(fù)一次或更多次
? 重復(fù)零次或一次
{n} 重復(fù)n次
{n,} 重復(fù)n次或更多次
{n,m} 重復(fù)n到m次

相關(guān)文章

最新評(píng)論