Skip to content

Commit 57d8330

Browse files
committed
Change email regex to not match local part with incorrect dots
According to official specifications which are summarised here https://en.wikipedia.org/wiki/Email_address local part of email can't be trailing or leading by "." character. It can't contain consecutive dots also. I've also added missing special characters support and added tests.
1 parent 56ecabb commit 57d8330

File tree

2 files changed

+26
-2
lines changed

2 files changed

+26
-2
lines changed

src/matcher/Email.js

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,11 @@ Autolinker.matcher.Email = Autolinker.Util.extend( Autolinker.matcher.Matcher, {
1919
*/
2020
matcherRegex : (function() {
2121
var alphaNumericChars = Autolinker.RegexLib.alphaNumericCharsStr,
22-
emailRegex = new RegExp( '[' + alphaNumericChars + '\\-_\';:&=+$.,]+@' ), // something@ for email addresses (a.k.a. local-part)
22+
specialCharacters = '!#$%&\'*+\\-\\/=?^_`{|}~',
23+
restrictedSpecialCharacters = '\\s"(),:;<>@\\[\\]',
24+
validCharacters = alphaNumericChars + specialCharacters,
25+
validRestrictedCharacters = validCharacters + restrictedSpecialCharacters,
26+
emailRegex = new RegExp( '(?:(?:[' + validCharacters + '](?![^@]*\\.\\.)(?:[' + validCharacters + '.]*[' + validCharacters + '])?)|(?:\\"[' + validRestrictedCharacters + '.]+\\"))@'),
2327
domainNameRegex = Autolinker.RegexLib.domainNameRegex,
2428
tldRegex = Autolinker.tldRegex; // match our known top level domains (TLDs)
2529

tests/matcher/EmailSpec.js

Lines changed: 21 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -94,7 +94,27 @@ describe( "Autolinker.matcher.Email", function() {
9494
expect( matches.length ).toBe( 0 );
9595
} );
9696

97+
it( 'should *not* match email with incorrect local part beginning with "."', function() {
98+
var matches = matcher.parseMatches( '.asdf@asdf.com' );
99+
100+
expect( matches.length ).toBe( 1 );
101+
MatchChecker.expectEmailMatch( matches[ 0 ], 'asdf@asdf.com', 1 );
102+
} );
103+
104+
it( 'should *not* match email with incorrect local part ending with "."', function() {
105+
var matches = matcher.parseMatches( 'asdf.@asdf.com' );
106+
107+
expect( matches.length ).toBe( 0 );
108+
} );
109+
110+
it( 'should match email skipping incorrect local part tailing with ".."', function() {
111+
var matches = matcher.parseMatches( 'asdf..asdf@asdf.com' );
112+
113+
expect( matches.length ).toBe( 1 );
114+
MatchChecker.expectEmailMatch( matches[ 0 ], 'asdf@asdf.com', 6 );
115+
} );
116+
97117
} );
98118

99119

100-
} );
120+
} );

0 commit comments

Comments
 (0)