@synotech/utils
Version:
a collection of utilities for internal use
16 lines (15 loc) • 508 B
text/typescript
/**
* This function extracts an email address from a string
* @module emailGetFromString
* @param {string} text - a string containing an email address
* @return {string} {String} an email address
* @example
*
* emailGetFromString('This is my email address: john@example.com')
*
*/
export function emailGetFromString(text: string): string | null {
const emailRegex = /[a-zA-Z0-9._%+-]+@[a-zA-Z0-9.-]+\.[a-zA-Z]{2,}/;
const match = RegExp(emailRegex).exec(text);
return match ? match[0] : null;
}