js string 함수 대전

Js 문자열 조작 함수 대전


String.prototype.LTrim = function()
{
        return this.replace(/(^\s*)/g, "");
}



String.prototype.Rtrim = function()
{
        return this.replace(/(\s*$)/g, "");
}

 


String.prototype.Trim = function()
{
        return this.replace(/(^\s*)|(\s*$)/g, "");
}


String.prototype.Left = function(len)
{

        if(isNaN(len)||len==null)
        {
                len = this.length;
        }
        else
        {
                if(parseInt(len)<0||parseInt(len)>this.length)
                {
                        len = this.length;
                }
        }
       
        return this.substr(0,len);
}



String.prototype.Right = function(len)
{

        if(isNaN(len)||len==null)
        {
                len = this.length;
        }
        else
        {
                if(parseInt(len)<0||parseInt(len)>this.length)
                {
                        len = this.length;
                }
        }
       
        return this.substring(this.length-len,this.length);
}



String.prototype.Mid = function(start,len)
{
        return this.substr(start,len);
}



String.prototype.InStr = function(str)
{

        if(str==null)
        {
                str = "";
        }
       
        return this.indexOf(str);
}


String.prototype.InStrRev = function(str)
{

        if(str==null)
        {
                str = "";
        }
       
        return this.lastIndexOf(str);
}

 


String.prototype.LengthW = function()
{
        return this.replace(/[^\x00-\xff]/g,"**").length;
}


String.prototype.isIP = function()
{

        var reSpaceCheck = /^(\d+)\.(\d+)\.(\d+)\.(\d+)$/;

        if (reSpaceCheck.test(this))
        {
                this.match(reSpaceCheck);
                if (RegExp.$1 <= 255 && RegExp.$1 >= 0
                 && RegExp.$2 <= 255 && RegExp.$2 >= 0
                 && RegExp.$3 <= 255 && RegExp.$3 >= 0
                 && RegExp.$4 <= 255 && RegExp.$4 >= 0)
                {
                        return true;    
                }
                else
                {
                        return false;
                }
        }
        else
        {
                return false;
        }
  
}



String.prototype.isLongDate = function()
{
        var r = this.replace(/(^\s*)|(\s*$)/g, "").match(/^(\d{1,4})(-|\/)(\d{1,2})\2(\d{1,2}) (\d{1,2}):(\d{1,2}):(\d{1,2})$/);
        if(r==null)
        {
                return false;
        }
        var d = new Date(r[1], r[3]-1,r[4],r[5],r[6],r[7]);
        return (d.getFullYear()==r[1]&&(d.getMonth()+1)==r[3]&&d.getDate()==r[4]&&d.getHours()==r[5]&&d.getMinutes()==r[6]&&d.getSeconds()==r[7]);

}


String.prototype.isShortDate = function()
{
        var r = this.replace(/(^\s*)|(\s*$)/g, "").match(/^(\d{1,4})(-|\/)(\d{1,2})\2(\d{1,2})$/);
        if(r==null)
        {
                return false;
        }
        var d = new Date(r[1], r[3]-1, r[4]);
        return (d.getFullYear()==r[1]&&(d.getMonth()+1)==r[3]&&d.getDate()==r[4]);
}


String.prototype.isDate = function()
{
        return this.isLongDate()||this.isShortDate();
}


String.prototype.isMobile = function()
{
        return /^0{0,1}13[0-9]{9}$/.test(this);
}


String.prototype.isEmail = function()
{
        return /^\w+((-\w+)|(\.\w+))*\@[A-Za-z0-9]+((\.|-)[A-Za-z0-9]+)*\.[A-Za-z0-9]+$/.test(this);
}

 

String.prototype.isZipCode = function()
{
        return /^[\\d]{6}$/.test(this);
}


String.prototype.existChinese = function()
{
        //[\u4E00-\u9FA5]   ﹐[\uFE30-\uFFA0]     
        return /^[\x00-\xff]*$/.test(this);
}


String.prototype.isFileName = function()
{
        return !/[\\\/\*\?\|:"<>]/g.test(this);
}


String.prototype.isUrl = function()
{
        return /^http[s]?:\/\/([\w-]+\.)+[\w-]+([\w-./?%&=]*)?$/i.test(this);
}



String.prototype.isIDCard = function()
{
        var iSum=0;
        var info="";
        var sId = this;

        var aCity={11:"  ",12:"  ",13:"  ",14:"  ",15:"   ",21:"  ",22:"  ",23:"   ",31:"  ",32:"  ",33:"  ",34:"  ",35:"  ",36:"  ",37:"  ",41:"  ",42:"  ",43:"  ",44:"  ",45:"  ",46:"  ",50:"  ",51:"  ",52:"  ",53:"  ",54:"  ",61:"  ",62:"  ",63:"  ",64:"  ",65:"  ",71:"  ",81:"  ",82:"  ",91:"  "};

        if(!/^\d{17}(\d|x)$/i.test(sId))
        {
                return false;
        }
        sId=sId.replace(/x$/i,"a");
        //    
        if(aCity[parseInt(sId.substr(0,2))]==null)
        {
                return false;
        }

        var sBirthday=sId.substr(6,4)+"-"+Number(sId.substr(10,2))+"-"+Number(sId.substr(12,2));

        var d=new Date(sBirthday.replace(/-/g,"/"))
       
        //    
        if(sBirthday!=(d.getFullYear()+"-"+ (d.getMonth()+1) + "-" + d.getDate()))
        {
                return false;
        }
        for(var i = 17;i>=0;i--)
        {
                iSum += (Math.pow(2,i) % 11) * parseInt(sId.charAt(17 - i),11);
        }

        if(iSum%11!=1)
        {
                return false;
        }
        return true;

}


String.prototype.isPhoneCall = function()
{
        return /(^[0-9]{3,4}\-[0-9]{3,8}$)|(^[0-9]{3,8}$)|(^\([0-9]{3,4}\)[0-9]{3,8}$)|(^0{0,1}13[0-9]{9}$)/.test(this);
}



String.prototype.isNumeric = function(flag)
{
        //       
        if(isNaN(this))
        {

                return false;
        }

        switch(flag)
        {

                case null:        //  
                case "":
                        return true;
                case "+":        //  
                        return                /(^\+?|^\d?)\d*\.?\d+$/.test(this);
                case "-":        //  
                        return                /^-\d*\.?\d+$/.test(this);
                case "i":        //  
                        return                /(^-?|^\+?|\d)\d+$/.test(this);
                case "+i":        //   
                        return                /(^\d+$)|(^\+?\d+$)/.test(this);                       
                case "-i":        //   
                        return                /^[-]\d+$/.test(this);
                case "f":        //   
                        return                /(^-?|^\+?|^\d?)\d*\.\d+$/.test(this);
                case "+f":        //    
                        return                /(^\+?|^\d?)\d*\.\d+$/.test(this);                       
                case "-f":        //    
                        return                /^[-]\d*\.\d$/.test(this);               
                default:        //  
                        return true;                       
        }
}


String.prototype.IsColor = function()
{
        var temp        = this;
        if (temp=="") return true;
        if (temp.length!=7) return false;
        return (temp.search(/\#[a-fA-F0-9]{6}/) != -1);
}


String.prototype.toCase = function()
{
        var tmp = "";
        for(var i=0;i<this.length;i++)
        {
                if(this.charCodeAt(i)>0&&this.charCodeAt(i)<255)
                {
                        tmp += String.fromCharCode(this.charCodeAt(i)+65248);
                }
                else
                {
                        tmp += String.fromCharCode(this.charCodeAt(i));
                }
        }
        return tmp
}


String.prototype.toHtmlEncode = function()
{
        var str = this;

        str=str.replace(/&/g,"&amp;");
        str=str.replace(/</g,"&lt;");
        str=str.replace(/>/g,"&gt;");
        str=str.replace(/\'/g,"&apos;");
        str=str.replace(/\"/g,"&quot;");
        str=str.replace(/
/g,"<br>"); str=str.replace(/\ /g,"&nbsp;"); str=str.replace(/\t/g,"&nbsp;&nbsp;&nbsp;&nbsp;"); return str; } String.prototype.toDate = function() { try { return new Date(this.replace(/-/g, "\/")); } catch(e) { return null; } } 1.Asc(x),Chr(x): , 2。Filter: :v=filter(x,s[,include[,compare]]) : Dim x()={"kjwang","wangkj","peter"} Dim v v=filter(x,"kj") ' v(0)="kjwang",v(1)="wangkj" v=filter(x,"kj",false) ' v(0)="peter" v=filter(x,"kj",true,vbTextCompare) ' 3.InStr: (InstrRev: ) : v=instr(x,y) ' x 1 y v=instr(n,x,y) ' x n y : v=InstrRev(x,s[,n[,Compare]]) 4。Join: :v=join(x[,d])’d 5。Len(x): x :v=len(x) 6.Left(x,n): x n ( Right(x,n)) 7。Mid: x :v=mid(x,n,m) 8。LTrim(x),RTim(x),Trim(x) 9.Replace: :v=Replace(x,s,r) :x="i saw a saw a saw" v=replace(x,"saw","so") 'v="i so a so a so" 10.Split: :v=split(s[,d]) :v=split("vb.net,iis6.0,asp.net",",") ' v(0)="vb.net",v(1)="iis6.0",v(2)="asp.net" 11.StrReverse: :v=strreverse("kjwang") 'v="gnawjk" 12.UCase(x),LCase(x): :x="hello,VB !" v=UCase(x) 'v="HELLO,VB !" 1. -1)Datue(x),Timue(x) :v=Datue(x) : “ ”    v=timue(x) ' -2)Year(x),Month(x),Day(x) :v=Year(x)    v=Month(x)    v=Day(x)    Hour(x),Minute(x),Second(x): , , -3)DateSerial(Y,M,D): 、 、 :Dim v v=DateSerial(1996,10,5) 'v=1996/10/5    TimeSerial(H,M,S): 、 、 2.Now: :v=now 3.Timer: 12:00:00AM :v=timer 4.DatePart(p,x): 、 、 :Dim X=#2/10/1996 16:45:30# v=DatePart("yyyy",X) 'v=1996 v=DatePart("m",X) 'v=2 v=DatePart("d",X) 'v=10 v=DatePart("h",X) 'v=16 v=DatePart("n",X) 'v=45 v=DatePart("s",X) 'v=30 v=DatePart("q",X) 'v=1 ( , ) v=DatePart("y",X) 'v=41 (1996 41 ) v=DatePart("ww",X) 'v=6 (1996 6 ) v=DatePart("w",X) 'v=7( 6 7 , ) 5。DateAdd(p,n,x): :v=DateAdd(p,n,x) 'p :"yyyy"、"m" :Dim x=#1/31/1996 16:45:30# v=dateadd("yyyy",-3,x) ' 3 ,v=1993/1/31 16:45:30 6.DateDiff(p,x1,x2): 'p :dim x1=#12/31/1996 16:45:30# x2=#2/10/1997 9:30:50# v=datediff("yyyy",x1,x2) 'v=1 7。FormatDateTime: :v=formatdateyime(x[, ]) :DateFormat.GeneralDate 、 DateFormat.LongDate、 DateFotmat.ShortDate、DateFormat.LongTime、DateFormat.ShortTime 8.MonthName: :v=monthname(5) 'v=" " 9.WeekDayName:  ’ 8.

좋은 웹페이지 즐겨찾기