国产探花免费观看_亚洲丰满少妇自慰呻吟_97日韩有码在线_资源在线日韩欧美_一区二区精品毛片,辰东完美世界有声小说,欢乐颂第一季,yy玄幻小说排行榜完本

首頁 > 編程 > JavaScript > 正文

javascript去掉前后空格的實例

2019-11-20 21:45:53
字體:
來源:轉載
供稿:網友

代碼

復制代碼 代碼如下:

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 去字符串空格 總結

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

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

復制代碼 代碼如下:

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

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

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

發表評論 共有條評論
用戶名: 密碼:
驗證碼: 匿名發表
主站蜘蛛池模板: 临沭县| 镇坪县| 永顺县| 蒙城县| 中西区| 大方县| 化德县| 凤冈县| 石楼县| 望城县| 新平| 宜宾县| 定安县| 庆云县| 十堰市| 福州市| 同江市| 突泉县| 汽车| 白河县| 东宁县| 璧山县| 临海市| 微博| 始兴县| 台中市| 咸宁市| 黄石市| 遂昌县| 吉林市| 龙州县| 通道| 凤台县| 陵川县| 罗城| 于都县| 腾冲县| 都兰县| 宕昌县| 鄱阳县| 淮安市|