[정규표현식] 핸드폰 번호 (+국가코드)

정유진·2022년 8월 8일
1

js

목록 보기
1/1
post-thumbnail

✏️ 목표

국가 번호 코드까지 입력할 수 있게 핸드폰 번호를 받자

^\s*(?:\+?(\d{1,3})[-. (]*)?\s*((01[016789]{3})[-. )]*)?((\d{3,4})[-. ]*)+(\d{4})+\s*$
+82 010 0000 0000
82 010 0000 0000
8201000000000
+82 010-0000-0000
82 010-0000-0000

📝 사용된 표현

^ the beginning of the string
\s white space
* quantifier 0 or more of the preceding token

(?:) Non-capturing group - multiple tokens together without creating a capture group
\+ Escaped, matches a + 
? quantifier 0 or 1 of the preceding token

() capturing group #1 creates a capture group of extracting a substring or using a backreference
\d digit (0-9)
{1,3} quantifier between 1 and 3

[] character set
+ quantifer 1 or more

$ the end of the string

🔍 참고 출처

https://regexr.com/

profile
느려도 한 걸음 씩 끝까지

0개의 댓글