zoukankan      html  css  js  c++  java
  • js 仿phptrim

     function trims(){
        this.init = function(myarguments){
            if(arguments.length===0){return false;}
            this.arg = myarguments;
            this.len = this.arg.length;
            if(this.len>0){ this.str = String(this.arg[0]); }
            if(this.len>1){ this.thechar = this.arg[1]; }
            if(typeof this.thechar=='undefined'){
                this.rg_l = new RegExp("^(\s|\u00A0)+");
                this.rg_r = new RegExp("\S");
            }else{
                this.rg_l = new RegExp("^("+this.thechar+")+");
                this.rg_r = new RegExp("[^"+this.thechar+"]{1}");    
            }        
        };
        if (typeof trims._initialized == "undefined") {
            trims.prototype.ltrim = function() {
              this.str = this.str.replace(this.rg_l,'');
            };
            trims.prototype.rtrim = function() {
                for(var i=this.str.length-1; i>=0; i--){
                    if(this.rg_r.test(this.str.charAt(i))){  
                        this.str = this.str.substring(0, i+1);  
                        break;
                    }
                }
                if(i===-1){this.str = '';}
            };
            trims._initialized = true;
        }  
    };
    var trimsobj = new trims();
    function trim(){
        trimsobj.init(arguments);
        trimsobj.ltrim();
        trimsobj.rtrim();
        return trimsobj.str;
    }
    function rtrim(){
        trimsobj.init(arguments);
        trimsobj.rtrim();
        return trimsobj.str;
    }
    function ltrim(){
        trimsobj.init(arguments);
        trimsobj.ltrim();
        return trimsobj.str;
    }
  • 相关阅读:
    第三次作业
    第二实验
    第一次作业
    yii2 Modal的使用
    yii2 显示列表字段 的技巧
    YII2在使用activeForm设置默认值
    html基础1
    tomcat+redis实现session共享缓存
    linux部署mongodb及基本操作
    hadoop 常用命令
  • 原文地址:https://www.cnblogs.com/goldenstones/p/5360329.html
Copyright © 2011-2022 走看看