zod icon indicating copy to clipboard operation
zod copied to clipboard

Safari Unsupported Email Regex

Open CameronB15 opened this issue 2 years ago • 4 comments

The email regex defined here https://github.com/colinhacks/zod/blob/master/src/types.ts#L518 is not supported on Safari.

// old version: too slow, didn't support unicode
// const emailRegex = /^((([a-z]|\d|[!#\$%&'\*\+\-\/=\?\^_`{\|}~]|[\u00A0-\uD7FF\uF900-\uFDCF\uFDF0-\uFFEF])+(\.([a-z]|\d|[!#\$%&'\*\+\-\/=\?\^_`{\|}~]|[\u00A0-\uD7FF\uF900-\uFDCF\uFDF0-\uFFEF])+)*)|((\x22)((((\x20|\x09)*(\x0d\x0a))?(\x20|\x09)+)?(([\x01-\x08\x0b\x0c\x0e-\x1f\x7f]|\x21|[\x23-\x5b]|[\x5d-\x7e]|[\u00A0-\uD7FF\uF900-\uFDCF\uFDF0-\uFFEF])|(\\([\x01-\x09\x0b\x0c\x0d-\x7f]|[\u00A0-\uD7FF\uF900-\uFDCF\uFDF0-\uFFEF]))))*(((\x20|\x09)*(\x0d\x0a))?(\x20|\x09)+)?(\x22)))@((([a-z]|\d|[\u00A0-\uD7FF\uF900-\uFDCF\uFDF0-\uFFEF])|(([a-z]|\d|[\u00A0-\uD7FF\uF900-\uFDCF\uFDF0-\uFFEF])([a-z]|\d|-|\.|_|~|[\u00A0-\uD7FF\uF900-\uFDCF\uFDF0-\uFFEF])*([a-z]|\d|[\u00A0-\uD7FF\uF900-\uFDCF\uFDF0-\uFFEF])))\.)+(([a-z]|[\u00A0-\uD7FF\uF900-\uFDCF\uFDF0-\uFFEF])|(([a-z]|[\u00A0-\uD7FF\uF900-\uFDCF\uFDF0-\uFFEF])([a-z]|\d|-|\.|_|~|[\u00A0-\uD7FF\uF900-\uFDCF\uFDF0-\uFFEF])*([a-z]|[\u00A0-\uD7FF\uF900-\uFDCF\uFDF0-\uFFEF])))$/i;
// eslint-disable-next-line
const emailRegex =
  /^(([^<>()[\].,;:\s@"]+(\.[^<>()[\].,;:\s@"]+)*)|(".+"))@((?!-)([^<>()[\].,;:\s@"]+\.)+[^<>()[\].,;:\s@"]{2,})(?<!-)$/i;

This is the problematic capture group: (?<!-). This is a negative lookbehind, which isn't supported on Safari https://caniuse.com/js-regexp-lookbehind. Sorry, I'm not sure what it should be re-written to, but I find that https://regexr.com/ is fantastic for testing regular expressions (and highlights the issue with the negative lookbehind too).

CameronB15 avatar Dec 12 '22 12:12 CameronB15

There is a WebKit PR open to implement lookbehind as well.

auskast avatar Dec 12 '22 19:12 auskast

I'm open to switching a regex without a negative look-behind. Open to proposals here.

colinhacks avatar Dec 12 '22 20:12 colinhacks

But we should revert https://github.com/colinhacks/zod/pull/1648 until there's a solution found since it silently breaks apps for safari users.

simPod avatar Dec 12 '22 20:12 simPod

Just reverted, landing in 3.20.2 momentarily

colinhacks avatar Dec 12 '22 21:12 colinhacks