예를 들어이 주소가 있다면;
820 10th Avenue New York, New York 10019
정규식에서 다음을 일치시키고 싶습니다.
820 10th Ave
820 10th Avenue
820 10th Avenue 뉴욕
위는 유일한 예상 형식입니다.
지금까지 내 코드
var re = /(^\d*\D*\w*)/i;
var str = '820 10th Avenue New York, New York 10019';
다음과 같이 작동합니다.
if(re.test('820 10th Avenue')) console.log('pass'); // pass
if(re.test('820 10th Ave')) console.log('pass'); // pass
if(re.test('820 10th')) console.log('pass'); // pass
if(!re.test('820 9th Ave')) console.log('fail'); // fail
if(!re.test('820')) console.log('fail'); // fail
입력 문자열이 주어진 문자열의 일부인지 확인하려면 다음을 사용할 수 있습니다 String.prototype.indexOf()
.
var str = '820 10th Avenue New York, New York 10019';
str.indexOf('820 10th Avenue') > -1 // true
str.indexOf('820 10th Ave') > -1 // true
str.indexOf('820 10th') > -1 // true
str.indexOf('820 9th Ave') > -1 // false
str.indexOf('820') > -1 // true
보다 구체적으로와 비교 0
하여 접두사인지 확인하십시오.
var str = '820 10th Avenue New York, New York 10019';
str.indexOf('820 10th Ave') === 0 // true
str.indexOf('20 10th Ave') === 0 // false
주어진 예에 따라 패턴 일치를 추가하십시오.
function testAddress(input) {
var address = '820 10th Avenue New York, New York 10019';
var re = /^\d+(\s\w+){2,}/;
return re.test(input) && address.indexOf(input) === 0;
}
testAddress('820 10th Ave') // true
testAddress('820 10th Avenue') // true
testAddress('820 10th Avenue New York') // true
testAddress('820 9th Ave') // false
testAddress('820 10th') // false
testAddress('820') // false
이 기사는 인터넷에서 수집됩니다. 재 인쇄 할 때 출처를 알려주십시오.
침해가 발생한 경우 연락 주시기 바랍니다[email protected] 삭제
몇 마디 만하겠습니다