create regex for email in typescript

To create a regex for email in TypeScript, you can use the following regular expression pattern:

index.ts
const emailRegex: RegExp = /^[^\s@]+@[^\s@]+\.[^\s@]+$/;
57 chars
2 lines

This regex pattern ensures that the email address has the format username@domain.extension, and does not contain any whitespace characters. You can then use this regex to validate email input in your TypeScript code, for example:

index.ts
function validateEmail(email: string): boolean {
  return emailRegex.test(email);
}

const isValidEmail: boolean = validateEmail('example@email.com'); // true
159 chars
6 lines

related categories

gistlibby LogSnag