Search code examples
javascriptregexjoi

Is there a way to add the ']' special character in my regex, when it normally ends my proposition?


I am trying to fit ']' into my regex that lets any string pass as long as it doesn't have a certain set of special characters. However, the IDE shows me that it ends the expression:

message: Joi.string().regex(/^[^<>#*=+^}[]+$/).required()
//current working code


message: Joi.string().regex(/^[^<>#*=+^}[]]+$/).required()
//what I am trying to add

Solution

  • You can escape special character using \

    So you can use: (/^[^<>#*=+^}[\]]+$/) which will escape the first ] allowing it to be viewed as a regular symbol.

    If you are new to regular expressions, try regex101.com. It does a good job of breaking them down for you.