Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 6 additions & 0 deletions packages/consts/src/regexs.ts
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,12 @@ export const EMAIL_REGEX_STR = `${nameSubRegexStr}@${domainSubRegexStr}`;
*/
export const EMAIL_REGEX = new RegExp(`^${EMAIL_REGEX_STR}$`);

/**
* Matches a string containing single email or multiple emails separated by comma
* Hostname must be a TLD! (will not match example@localhost)
*/
export const COMMA_SEPARATED_EMAILS_REGEX = new RegExp(`^(${EMAIL_REGEX_STR})( *, *${EMAIL_REGEX_STR})*$`, 'g');

/**
* Comes from https://github.com/jonschlinkert/is-git-url/ but we have:
* - added support for ...:/dir/subdir syntax
Expand Down
39 changes: 39 additions & 0 deletions test/regexs.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -140,6 +140,45 @@ const tests = {
],
},

COMMA_SEPARATED_EMAILS_REGEX: {
valid: [
'test@example.com',
'test@example.com,foo@example.com',
'test@example.com, foo@example.com',
'test@example.com ,foo@example.com',
'test@example.com , foo@example.com,bar@example.com',
'test@example.com,a.b.c+123~@example.com,bar@example.com',
],
invalid: [
'',
'not an email',
'not, an, email',
'not-an-email@',
'not-an-email@,',
'test@example.com ,@example.com',
'test@example.com,,foo@example.com',
'test@example.com,foo@example.com.',
'test@example.com,... foo@example.com',
'test@example.com, foo@example,com',
'test@example.com\n,foo@example.com',
'test@example.com,foo@example.com\n',
'\ntest@example.com,foo@example.com',
'test@example.com\t,foo@example.com',
'test@example.com ,foo@example.com ',
'not-an-email,test@example.com',
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

maybe let's add empty string? and some other extreme emails? newlines? tabs? e.g. text with no @ etc.

'test@example.com, not-an-email',
' test@example.com',
'test@example.com foo@example.com',
'test@example.comfoo@example.com',
',test@example.com ,foo@example.com',
'test@example.com,foo.@example.com',
'test@example.com,foo@example.com,bar@example.com,',
'test@example.com,foo@example.com,bar@example.com,fff@.com',
'test@example.com,a.b.c+123~@example.com,bar@@example.com',
'test@example.com,a.b.c+123~@example_foo.com,bar@@example.com',
],
},

// Test samples inspired by https://mathiasbynens.be/demo/url-regex
HTTP_URL_REGEX: {
valid: [
Expand Down