숫자를 직접 끌어 내기 위해 언제든지 사용할 수 있습니다.
는
는 커피 스크립트 :
console.log number.match ///
# Match the start of the string.
^
# Get the first 3 digits.
\(?(?=\d{3})(\d{3})[).\-\s]*
# Get the next 3 digits.
(\d{3})[.\-\s]*
# Get the last 4 digits
(\d{4})
# End of the number
$
///
자바 스크립트 (또는 노드) : 여기
console.log(number.match(/^\(?(?=\d{3})(\d{3})[).\-\s]*(\d{3})[.\-\s]*(\d{4})$/));
만들기 위해 내가 커피 스크립트에 실행 된 테스트 케이스를 있는지 그것은 일 :
exports '1234567890'
exports '123 456 7890'
exports '123.456.7890'
exports '123 456.7890'
exports '123.456 7890'
exports '123456.7890'
exports '123.4567890'
exports '123456. 7890'
exports '123-456-7890'
exports '123 456-7890'
exports '123-456 7890'
exports '123456-7890'
exports '123-4567890'
exports '(123)456-7890'
exports '(123)4567890'
exports '(123) 4567890'
exports '(123)456 7890'
exports '(123) 456 7890'
exports '(123) 4567890'
그리고 그들의 출력 :
1234567890
[ '1234567890',
'123',
'456',
'7890',
index: 0,
input: '1234567890' ]
123 456 7890
[ '123 456 7890',
'123',
'456',
'7890',
index: 0,
input: '123 456 7890' ]
123.456.7890
[ '123.456.7890',
'123',
'456',
'7890',
index: 0,
input: '123.456.7890' ]
123 456.7890
[ '123 456.7890',
'123',
'456',
'7890',
index: 0,
input: '123 456.7890' ]
123.456 7890
[ '123.456 7890',
'123',
'456',
'7890',
index: 0,
input: '123.456 7890' ]
123456.7890
[ '123456.7890',
'123',
'456',
'7890',
index: 0,
input: '123456.7890' ]
123.4567890
[ '123.4567890',
'123',
'456',
'7890',
index: 0,
input: '123.4567890' ]
123456. 7890
[ '123456. 7890',
'123',
'456',
'7890',
index: 0,
input: '123456. 7890' ]
123-456-7890
[ '123-456-7890',
'123',
'456',
'7890',
index: 0,
input: '123-456-7890' ]
123 456-7890
[ '123 456-7890',
'123',
'456',
'7890',
index: 0,
input: '123 456-7890' ]
123-456 7890
[ '123-456 7890',
'123',
'456',
'7890',
index: 0,
input: '123-456 7890' ]
123456-7890
[ '123456-7890',
'123',
'456',
'7890',
index: 0,
input: '123456-7890' ]
123-4567890
[ '123-4567890',
'123',
'456',
'7890',
index: 0,
input: '123-4567890' ]
(123)456-7890
[ '(123)456-7890',
'123',
'456',
'7890',
index: 0,
input: '(123)456-7890' ]
(123)4567890
[ '(123)4567890',
'123',
'456',
'7890',
index: 0,
input: '(123)4567890' ]
(123) 4567890
[ '(123) 4567890',
'123',
'456',
'7890',
index: 0,
input: '(123) 4567890' ]
(123)456 7890
[ '(123)456 7890',
'123',
'456',
'7890',
index: 0,
input: '(123)456 7890' ]
(123) 456 7890
[ '(123) 456 7890',
'123',
'456',
'7890',
index: 0,
input: '(123) 456 7890' ]
(123) 4567890
[ '(123) 4567890',
'123',
'456',
'7890',
index: 0,
input: '(123) 4567890' ]
number.match
이 해당 코드로 null
을 반환하면 올바른 숫자가 아닙니다. 따라서 쉽게 확인할 수 있습니다. 그런 다음 이미 번호가 파싱되어 있습니다.
a-z
을 허용하려면 \d
항목을 모두 [\d\w]
으로 변경하십시오. (See this)
누군가가 값을 입력했는지만으로 문제가 발생 했습니까? 뭐예요 누군가가 "셀 : # 555-555 (5005)"로 입력하지 않을 것이라고 말합니다 – Steven
우연히 이메일 주소를 누군가 입력하면 어떻게 될까요? 그러면 내 데이터 구조가 모두 엉망이 될 것입니다 !! –
"1-800-MATTRES"는 어떻습니까? 그게 유효한 전화 번호 야, 안 그래? :-D –