Q:

Joi - Invalid Regular Expression

/^(?! )(?=.*[a-z])(?=.*[A-Z])(?=.*\d)([a-zA-Z\d!"#$%&'()*+,-./:;<=>?@[\]^_`{|}~ ]{8,})(?<! )$/

Works perfectly in regex101.com, chrome console and even joi npm runkit. But when used in code Joi is giving error as -
SyntaxError: Invalid regular expression: {above regex}: Invalid Group

Can u please help me with this??

A:

Note that browsers that do not support ECMAScript 2018 do not support lookbehinds in regular expressions. 
The pattern you have contains (?<! ) negative lookbehind that checks if there is no space at the end of the string (it stands before $ anchor).
Hence, you may fix it by replacing that lookbehind with (?=.*\S$) (requires a non-whitespace char at the end of the string) or (?!.*\s$) (disallows a whitespace at the end of the string) lookaheads that are supported by the popular ES5 standard. 

