오늘도 한 뼘 더
[JavaScript] 전화번호 서식(-) 코드 본문
728x90
반응형
# 배경
핸드폰 번호외에도 일반 전화번호를 "-"로 마스킹 하는 함수가 필요하다
# 코드
- 앞에 두자리가 02인지 아닌지 확인
- 02의 경우 9자리 - 10자리
- 이외에는 10자리 - 11자리
const text = "021234567"
const maskTelNo = (text) =>
text && text.length <= 11
? text.substr(0, 2) === "02"
? text.length === 9
? text.substring(0, 2) + "-" + text.substring(2, 5) + "-" + text.substring(5, text.length)
: text.substring(0, 2) + "-" + text.substring(2, 6) + "-" + text.substring(6, text.length)
: text.length === 10
? text.substring(0, 3) + "-" + text.substring(3, 6) + "-" + text.substring(6, text.length)
: text.substring(0, 3) + "-" + text.substring(3, 7) + "-" + text.substring(7, text.length)
: text;
console.log(maskTelNo(text)) // "02-123-4567"
728x90
반응형
'Study > JavaScript & React.js' 카테고리의 다른 글
[JavaScript] replace로 문자열 치환하기 (0) | 2022.10.11 |
---|---|
[React] Error : Attempted import error: 'create' is not exported from 'fontkit' (imported as 'fontkit'). (0) | 2022.08.31 |
Comments