본문 바로가기
카테고리 없음

전화번호 정규식

by 찬찬2 2021. 3. 8.

 

  function phoneFomatter(num,type){
    var formatNum = '';

    if(num.length==11){
        if(type==0){
            formatNum = num.replace(/(\d{3})(\d{4})(\d{4})/, '$1-****-$3');
        }else{
            formatNum = num.replace(/(\d{3})(\d{4})(\d{4})/, '$1-$2-$3');
        }
    }else if(num.length==8){
        formatNum = num.replace(/(\d{4})(\d{4})/, '$1-$2');
    }else{
        if(num.indexOf('02')==0){
            if(type==0){
                formatNum = num.replace(/(\d{2})(\d{4})(\d{4})/, '$1-****-$3');
            }else{
                formatNum = num.replace(/(\d{2})(\d{4})(\d{4})/, '$1-$2-$3');
            }
        }else{
            if(type==0){
                formatNum = num.replace(/(\d{3})(\d{3})(\d{4})/, '$1-***-$3');
            }else{
                formatNum = num.replace(/(\d{3})(\d{3})(\d{4})/, '$1-$2-$3');
            }
        }
    }

    return formatNum;
}





2. 사용 예제

 - 역시 너무 간단하다. 이래도 되나 싶다

phoneFomatter('01000000000');   //010-0000-0000

phoneFomatter('01000000000',0); //010-****-0000

phoneFomatter('0100000000');    //010-000-0000

phoneFomatter('0100000000',0);  //010-***-0000

phoneFomatter('0200000000');    //02-0000-0000

phoneFomatter('0200000000',0);  //02-****-0000

phoneFomatter('0310000000');    //031-000-0000

phoneFomatter('0310000000',0);  //031-***-0000

phoneFomatter('16880000');      //1688-0000

댓글