Skip to content

Commit

Permalink
fix(index.ts): RegEx running slow on strings with many repetitions of…
Browse files Browse the repository at this point in the history
… '!.'.
  • Loading branch information
Codycody31 committed Dec 13, 2023
1 parent a267a47 commit bf51318
Showing 1 changed file with 3 additions and 4 deletions.
7 changes: 3 additions & 4 deletions src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -172,10 +172,9 @@ class Validator {
* @returns {boolean} Returns true if the email is valid, otherwise false.
*/
static isEmail(email: string): boolean {
// We only need to check for the presence of @ and .
// This ensures that the email is at least in the correct format.
// But going beyond this is not recommended as it is very difficult to validate an email address correctly.
const re = /^[^\s@]+@[^\s@]+\.[^\s@]+$/;
// This regular expression is more efficient and less prone to backtracking issues.
// It simplifies the check for an email format by eliminating unnecessary repetition patterns.
const re = /^[^\s@]+@[^\s@]+\.[^\s@]{2,}$/;
return re.test(String(email).toLowerCase());
}

Expand Down

0 comments on commit bf51318

Please sign in to comment.