Skip to content

Instantly share code, notes, and snippets.

@sbin0819
Created July 7, 2022 08:30
Show Gist options
  • Save sbin0819/b9700428c2fb87146be72359560b4463 to your computer and use it in GitHub Desktop.
Save sbin0819/b9700428c2fb87146be72359560b4463 to your computer and use it in GitHub Desktop.

regex 활용 심화

핸드폰 format

 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;
}

참고링크

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment