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;
    }
  • 相关阅读:
    Grove.net实践ORM学习笔记
    COM+的事务
    Delphi中MIDAS线程模型
    Delphi中封装ADO之我重学习记录。。。
    100 多个JaveScript 常用函数
    javascript 事件
    js 收藏
    js 常用函数
    表单11种Input的高级用法
    UltraEdit 使用技巧
  • 原文地址:https://www.cnblogs.com/goldenstones/p/5360329.html
Copyright © 2011-2022 走看看