이 때문에 경기의 시작 부분에 ^
의의 :
/(www\.)?([^@])([a-z]*\.)(com|net|edu|org)(\.au)?(\/\S*)?$/g
js> "www.foobar.com".match(/(www\.)?([^@])([a-z]*\.)(com|net|edu|org)(\.au)?(\/\S*)?$/g)
["www.foobar.com"]
js> "aoeuaoeu foobar.com".match(/(www\.)?([^@])([a-z]*\.)(com|net|edu|org)(\.au)?(\/\S*)?$/g)
[" foobar.com"]
js> "[email protected] foobar.com".match(/(www\.)?([^@])([a-z]*\.)(com|net|edu|org)(\.au)?(\/\S*)?$/g)
[" foobar.com"]
js> "[email protected] [email protected]".match(/(www\.)?([^@])([a-z]*\.)(com|net|edu|org)(\.au)?(\/\S*)?$/g)
["foobar.com"]
를 여전히 도메인 앞에 공백을 일치 비록. 그리고 그것은 도메인에 대한 잘못된 가정을하고 있습니다 ...
xyz.example.org
은 정규 표현식과 일치하지 않는 유효한 도메인입니다.
www.3x4mpl3.org
은 regexp와 (과) 일치하지 않는 유효한 도메인입니다.
example.co.uk
은 정규 표현식과 일치하지 않는 유효한 도메인입니다.
ουτοπία.δπθ.gr
은 정규 표현식과 일치하지 않는 유효한 도메인입니다.
무엇이 합법적 인 도메인 이름을 정의합니까? 이것은 점으로 구분 된 UTF-8 문자의 연속입니다. 서로 뒤 따르는 두 개의 점을 가질 수 없으며 표준 이름은 \w\.\w\w
입니다 (하나의 문자가 있다고는 생각하지 않습니다).
비록, 내가 할 줄 방법은 단순히 는 점 분리하여 단어 경계 텍스트 (\b
) 모든 것을 고려하여, 같은 도메인을 보이는 모든 일치하는 것입니다
/\b(\w+\.)+\w+\b/g
을
js> "aoe toto.example.org uaoeu foo.bar aoeuaoeu".match(/\b(\w+\.)+\w+\b/g)
["toto.example.org", "foo.bar"]
js> "aoe [email protected] toto.example.org uaoeu foo.bar aoeuaoeu".match(/\b(\w+\.)+\w+\b/g)
["example.org", "toto.example.org", "foo.bar"]
js> "aoe [email protected] toto.example.org uaoeu foo.bar aoeuaoeu f00bar.com".match(/\b(\w+\.)+\w+\b/g)
["example.org", "toto.example.org", "foo.bar", "f00bar.com"]
두 번째 라운드에서 도메인이 실제로 존재하는지 여부를 확인하거나 발견 된 도메인 목록에 있는지 확인하십시오.단점은 자바 스크립트에서 regexps는 유니 코드 문자를 검사 할 수 없으며 \b
또는 \w
은 유효한 도메인 이름으로 ουτοπία.δπθ.gr
을 수락하지 않습니다. ES6에서
는, (내가 지금까지 테스트 한하지만 없음) 최신 브라우저에서 작동해야 /u
modifier, 거기 :
"ουτοπία.δπθ.gr aoe [email protected] toto.example.org uaoeu foo.bar aoeuaoeu".match(/\b(\w+\.)+\w+\b/gu)
편집 :
부정적인 lookbehind 그것을 해결 - 하지만 분명히 JS에 없습니다.
네, 그것은 것입니다 : 모든 전자 메일 주소를 건너 뛰는, 여기에 정규식 구현 뒤에 작업 모습입니다 :
/(?![^@])?\b(\w+\.)+\w+\b/g
js> "aoe [email protected] toto.example.org uaoeu foo.bar aoeuaoeu f00bar.com".match(/(?<![^@])?\b(\w+\.)+\w+\b/g)
["toto.example.org", "foo.bar", "f00bar.com"]
는 유니 코드와 같은 비록 ... 그것 ' JS에 곧있을거야.
일치하는 정규 표현식에 @
을 실제로 보존하고 일치하는 항목을 버리면됩니다. 그는 @ 포함
js> "toto.net aoe [email protected] toto.example.org uaoeu foo.bar aoeuaoeu f00bar.com".match(/@?\b\w+\.+\w+\b/g).map(function (x) { if (!x.match(/@/)) return x })
["toto.net", (void 0), "toto.example", "foo.bar", "f00bar.com"]
또는 현대 브라우저가 있어야 ES6/JS1.7의 새로운 지능형리스트를 사용을 ...
[x for x of "toto.net aoe [email protected] toto.example.org uaoeu foo.bar aoeuaoeu f00bar.com".match(/@?\b\w+\.+\w+\b/g) if (!x.match(/@/))];
일 최종 업데이트 :
/@?\b(\w*[^\W\d]+\w*\.+)+[^\W\d_]{2,}\b/g
> "x.y tot.toc.toc $11.00 11.com 11foo.com toto.11 toto.net aoe [email protected] toto.example.org uaoeu foo.bar aoeuaoeu f00bar.com".match(/@?\b(\w*[^\W\d]+\w*\.+)+[^\W\d_]{2,}\b/g).filter(function (x) { if (!x.match(/@/)) return x })
[ 'tot.toc.toc',
'11foo.com',
'toto.net',
'toto.example.org',
'foo.bar',
'f00bar.com' ]
[질문] (http://stackoverflow.com/questions/641407/javascript-negative-lookbehind-equivalent) * 도움이 될 수 있습니다. – merlin2011
사이드 노트 : 사용 가능한 또는 곧 제공 될 새로운 TLD의 엄청난 양을 알고 있습니까? – Marty
http://regexr.com/ – HJ05