zoukankan      html  css  js  c++  java
  • JavaScript:字符串の空格刪減和字符刪減功能

    /* 刪除左右空格 */
    function trim(string){return string.replace(/(^s*)|(s*$)/g,'');};  //函數法
    String.prototype.__defineGetter__("trim",function(){return this.replace(/(^s*)|(s*$)/g,'')})  //prototype法
    
    /* 刪除左側空格 */
    function ltrim(string){return string.replace(/^s+/,'');};  //函數法
    String.prototype.__defineGetter__("ltrim",function(){return this.replace(/^s+/,'')})  //prototype法
    
    /* 刪除右側空格 */
    function rtrim(string){return string.replace(/s+$/,'');};  //函數法
    String.prototype.__defineGetter__("rtrim",function(){return this.replace(/s+$/,'')})  //prototype法
    
    /* 刪除所有空格 */
    function alltrim(string){return string.replace(/s/g,'');};  //函數法
    String.prototype.__defineGetter__("alltrim",function(){return this.replace(/s/g,'')})  //prototype法
    
    /* 刪除指定字符 */
    function delChar(string,char){var charReg=new RegExp(char,'ig');return string.replace(charReg,'');}
    
    //prototype法使用說明
    var a=" a b c d e ";
    console.log(a.ltrim);  //輸出"a b c d e "
    console.log(a.rtrim);  //輸出" a b c d e"
    console.log(a.trim);   //輸出"a b c d e"
    console.log(a.alltrim); //輸出"abcde"
  • 相关阅读:
    Oracle存储过程格式
    Parallel并行运算实例
    唐让的领航少年
    株洲县阳光三农网
    株洲县招商网
    利用css新属性appearance优化select下拉框
    谈谈我的出差感想
    颜色表及html代码
    jquery中DOM的操作方法
    HTML DOM的nodeName,nodeValue,nodeType介绍
  • 原文地址:https://www.cnblogs.com/mandongpiaoxue/p/10494626.html
Copyright © 2011-2022 走看看