Skip to content
Open
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
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
---
keyword: email
---

The email pattern is sourced from the [HTML spec](https://html.spec.whatwg.org/multipage/input.html#valid-e-mail-address).

```
/^[a-zA-Z0-9.!#$%&'*+\/=?^_`{|}~-]+@[a-zA-Z0-9](?:[a-zA-Z0-9-]{0,61}[a-zA-Z0-9])?(?:\.[a-zA-Z0-9](?:[a-zA-Z0-9-]{0,61}[a-zA-Z0-9])?)*$/
```

## How to use

```typescript
import { emailPattern } from '@studiohyperdrive/regex-common';

const isValidEmail = emailPattern.test('simple@example.com');
```
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
import { NgDocPage } from '@ng-doc/core';
import { ExpressionsCategory } from '../../../../../../categories/javascript';

const emailPage: NgDocPage = {
title: `email`,
mdFile: './index.md',
category: ExpressionsCategory,
order: 1,
};

export default emailPage;
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ describe('phoneBelgiumBinFormatted', () => {
expect(bePhoneBinFormatted.mobilePattern.test('0470 12 34 56')).toBe(true);
});

it('should not test true to non-BIN formatted mobile phone numbers;', () => {
it('should not test true to non-BIN formatted mobile phone numbers', () => {
expect(bePhoneBinFormatted.mobilePattern.test('003212345678')).toBe(false);
expect(bePhoneBinFormatted.mobilePattern.test('+3212345678')).toBe(false);
expect(bePhoneBinFormatted.mobilePattern.test('012345678')).toBe(false);
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,45 @@
import { describe } from 'vitest';

import { emailPattern } from './email';

describe('emailPattern', () => {
it('should test true to email addresses that comply', () => {
// https://en.wikipedia.org/wiki/Email_address#Valid_email_addresses
[
'simple@example.com',
'very.common@example.com',
'FirstName.LastName@EasierReading.org',
'x@example.com',
'long.email-address-with-hyphens@and.subdomains.example.com',
'user.name+tag+sorting@example.com',
'user.name@example.com',
'name/surname@example.com',
'admin@example',
'example@s.example',
'" "@example.org',
'"john..doe"@example.org',
'mailhost!username@example.org',
'"very.(),:;<>[]\\".VERY.\\"very@\\\\ \\"very\\".unusual"@strange.example.com',
'user%example.com@example.org',
'user-@example.org',
'postmaster@[123.123.123.123]',
'postmaster@[IPv6:2001:0db8:85a3:0000:0000:8a2e:0370:7334]',
'_test@[IPv6:2001:0db8:85a3:0000:0000:8a2e:0370:7334]',
'I❤️CHOCOLATE@example.com',
].forEach((value) => expect(emailPattern.test(value)).toBe(true));
});

it('should not test true to email addresses that do not comply', () => {
// https://en.wikipedia.org/wiki/Email_address#Invalid_email_addresses
[
'abc.example.com',
'a@b@c@example.com',
'a"b(c)d,e:f;g<h>i[j\\k]l@example.com',
'just"not"right@example.com',
'this is"not\\allowed@example.com',
'this\\ still\\"not\\\\allowed@example.com',
'1234567890123456789012345678901234567890123456789012345678901234+x@example.com',
'i.like.underscores@but_they_are_not_allowed_in_this_part',
].forEach((value) => expect(emailPattern.test(value)).toBe(false));
});
});
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
/* eslint-disable no-useless-escape */

/**
* The html-spec email regex pattern.
* https://html.spec.whatwg.org/multipage/input.html#valid-e-mail-address
*/
export const emailPattern =
/^[a-zA-Z0-9.!#$%&'*+\/=?^_`{|}~-]+@[a-zA-Z0-9](?:[a-zA-Z0-9-]{0,61}[a-zA-Z0-9])?(?:\.[a-zA-Z0-9](?:[a-zA-Z0-9-]{0,61}[a-zA-Z0-9])?)*$/;
1 change: 1 addition & 0 deletions libs/javascript/regex/src/lib/patterns/technical/index.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
export * from './email/email';