mikeerickson / validatorjs

A data validation library in JavaScript for the browser and Node.js, inspired by Laravel's Validator.
https://www.npmjs.com/package/validatorjs
MIT License
1.77k stars 280 forks source link

An error with a valid regexp #443

Open 12rita opened 2 years ago

12rita commented 2 years ago

Hi, i'm trying to validate an ip adress, and i'm using a regexp type of rule. But i get an error: Uncaught SyntaxError: Invalid regular expression: /^(25[0-5/: Unterminated character class. I opened the function that throws an error, copied it and tried to check my regexp by myself and it worked fine, no errors. My regular expression: regex:/^(?:(?:25[0-5]|2[0-4][0-9]|[01]?[0-9][0-9]?)\\.){3}(?:25[0-5]|2[0-4][0-9]|[01]?[0-9][0-9]?)(\\/(\\d){1,2})?$/

I've tried to check within this function: `regex: function (val, req) { let reqPattern = req; var mod = /[g|i|m]{1,3}$/; var flag = req.match(mod); flag = flag ? flag[0] : "";

req = req.replace(mod, "").slice(1, -1);
req = new RegExp(req, flag);
return !!req.test(val);

},`

I am willing to figure out, what is going wrong here

I'm using 3.22.1 version of validator js

RafidMuhymin commented 1 year ago

@12rita I think it's happening because your regex contains the | character. Try using array format instead of string format as shown in this example.