INNER CODE UNIT · TypeScript
WORD_CHAR_REGEX
postalsys/postal-mime · src/address-parser.ts:19
const WORD_CHAR_REGEX = /\w/;
const NON_SPACE_TOKEN_REGEX = /[^\s]+/g;
/**
* Finds the first address looking token in a run of text.
*
* This replaces a `\s*\b[^@\s]+@[^\s]+\b\s*` scan over the whole string, which backtracks
* quadratically: the leading `\s*` makes every position inside a whitespace run a viable
* start, and `[^@\s]+` then gives back one character at a time looking for an '@'. A
* single header well inside the default size limit could hold a core busy for minutes.
*
* Scanning whitespace delimited tokens instead is linear and keeps the word boundary
* semantics of the regex: the local part has to open on a word character and the domain
* has to end on one.
*
* @param text Text to search
* @return `{index, length, value}` of the address, or null if there is none
*/