[JS] Javascirpt 를 이용한 각종 정규식_마스킹 방법

2019. 10. 28. 09:28
반응형

[JS] Javascirpt 를 이용한 각종 정규식_마스킹 방법



회원정보 및 개인정보등을 정규식으로 걸러내어 사용할경우가많다


보통 input 박스안에있는 데이터를 많이 사용하는데


이때에는 보통 유효성을 많이 체크한다.


이럴때도 사용가능하지만


이번엔 textarea 안에서 해당 값을 찾아내는법을 해볼까한다.


match 를 이용하는법이다


아래 소스들을 참고하길 바란다.


▼Email


1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
 var OriginData = $OriginData.val();
 
var findMasking = "*";
var ContentsData;
var len;
 
 
                //email masking
                var emailsArray = OriginData.match(/([a-zA-Z0-9._-]+@[a-zA-Z0-9._-]+\.[a-zA-Z0-9._-]+)/gi);
 
                if(emailsArray== null || emailsArray =="")
                {
                    ContentsData = OriginData;
                }
                else
                {
                    len = emailsArray.toString().split('@').length;
                    ContentsData = OriginData.toString().replace(new RegExp('.(?=.{0,' + len + '}@)''g'), '*');
                }
 
 
// email1234@daum.net
// email1***@daum.net
cs




▼Card

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
  // card  masking _16
                var cardArray = ContentsData.match(/(\d{4})-(\d{4})-(\d{4})-(\d{4})/gi);
                if(cardArray== null || cardArray =="")
                {
                    ContentsData = ContentsData;
                }
                else
                {
for (var i = 0; i <cardArray.length ; i++)
{
                    ContentsData = ContentsData.toString().replace(cardArray[i],cardArray[i].toString().replace(/(\d{4})-(\d{4})-(\d{4})-(\d{4})/gi,"$1-****-****-$4"));
                 }
}
 
                cardArray = ContentsData.match(/(5[1-5]\d{14})|(4\d{12})(\d{3}?)|3[47]\d{13}|(6011\d{12})/gi);
                if(cardArray== null || cardArray =="")
                {
                    ContentsData = ContentsData;
                }
                else
                {
                    
for (var i = 0; i <cardArray.length ; i++)
{
                    ContentsData = ContentsData.toString().replace(cardArray[i],cardArray[i].toString().replace(/(\d{4})(\d{4})(\d{4})(\d{4})/gi,"$1********$4"));
                 }
                }
 

// 4000-1234-5678-0000
// 4000-****-****-0000
cs





▼주민등록번호


1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
// National identification Number masking_13
                var ninArray = ContentsData.match(/(?:[0-9]{2}(?:0[1-9]|1[0-2])(?:0[1-9]|[1,2][0-9]|3[0,1]))-[1-4]{1}[0-9]{6}\b/gi);
                if(ninArray== null || ninArray =="")
                {
                    ContentsData = ContentsData;
                }
                else
                {
                    len = ninArray.toString().split('-').length;
                    ContentsData = ContentsData.toString().replace(ninArray,ninArray.toString().replace(/-?([1-4]{1})([0-9]{5})$/gi,"******"));
                }
 
                ninArray = ContentsData.match(/\d{13}/gi);
                if(ninArray== null || ninArray =="")
                {
                    ContentsData = ContentsData;
                }
                else
                {
                    ContentsData = ContentsData.toString().replace(ninArray,ninArray.toString().replace(/([0-9]{6})$/gi,"******"));
                }

// 000101-1234567
// 000101-1******

\
cs



▼phone

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
// phone masking_11
                var phoneArray = ContentsData.match(/\d{2,3}-\d{3,4}-\d{4}/gi);
 
                if(/-[0-9]{3}-/.test(phoneArray))
                { // 00-000-0000 형태인 경우
                    for (var i = 0; i <phoneArray.length ; i++) {
                        alert(phoneArray[i]);
                    ContentsData= ContentsData.toString().replace(phoneArray[i],phoneArray[i].toString().replace(/-[0-9]{3}-/g, "-***-"));
                    }
                }
                else if(/-[0-9]{4}-/.test(phoneArray))
                { // 00-0000-0000 형태인 경우
                    for (var i = 0; i <phoneArray.length ; i++) {
                        alert(phoneArray[i]);
 
                    ContentsData= ContentsData.toString().replace(phoneArray[i],phoneArray[i].toString().replace(/-[0-9]{4}-/g, "-****-"));
                    }
                }
 
 
 
                phoneArray = ContentsData.match(/\d{11}/gi);
 
                if(phoneArray== null || phoneArray =="")
                {
                    ContentsData = ContentsData;
                }
                else
                {
 
 
                    ContentsData = ContentsData.toString().replace(phoneArray[i],phoneArray[i].toString().replace(/(\d{3})(\d{4})(\d{4})/gi,'$1****$3'));
 
                }


// 010-0000-0000
// 010-****-0000
cs




▼계좌번호

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
// account Masking_10
                var accountArray = ContentsData.match(/\d{10}/gi);
                alert(accountArray);
                if(accountArray== null || accountArray =="")
                {
                    ContentsData = ContentsData;
                }
                else
                {
                    ContentsData = ContentsData.toString().replace(accountArray,accountArray.toString().replace(/([0-9]{6})$/gi,'******'));
                }
 
 
                // car masking_11
                var accountArray = ContentsData.match(/[0-9]{2}[가~힣]{1}[\s]*[0-9]{4}/gi);
                alert(accountArray);
                if(accountArray== null || accountArray =="")
                {
                    ContentsData = ContentsData;
                }
                else
                {
                    ContentsData = ContentsData.toString().replace(accountArray,accountArray.toString().replace(/([0-9]{6})$/gi,'******'));
                }
cs



반응형

BELATED ARTICLES

more