prem topno
prem topno

Reputation: 23

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??

Upvotes: 2

Views: 384

Answers (1)

Wiktor Stribiżew
Wiktor Stribiżew

Reputation: 626802

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.

Upvotes: 2

Related Questions