0

I want to check for percentage between 20 to 50 (both included and decimals are allowed as well) in my JS code.

I created a regex for this /^[2-4]\d(\.\d\d?)?|50(\.00?)?$/

This regex successfully works, but I need to add this to JSON file, so I'm escaping it.

Escaped regex is /^[2-4]\\d(\\.\\d\\d?)?|50(\\.00?)?$/

Now the issue is that escaped regex is not working to detect the required values.

Any help is appreciated

iamsaksham
  • 2,879
  • 4
  • 26
  • 50
  • 1
    Double escaped regex should be `^(?:[2-4]\\d(?:\\.\\d\\d?)?|50(?:\\.00?)?)$` – anubhava Aug 26 '22 at 05:48
  • 1
    Please show exactly how it occurs in JSON, and what you do with it when you parse the JSON. It should be possible to make a runnable snippet (use the toolbar) with an actual (very small) JSON and the code that parses it, and executes the regex, and reproduces the issue. – trincot Aug 26 '22 at 05:51
  • @trincot "inputRegex": "/^[2-4]\\d(\\.\\d\\d?)?|50(\\.00?)?$/", this is how it looks like in the JSON file – iamsaksham Aug 26 '22 at 05:52
  • Can you edit your question and maybe even create a runnable snippet using the toolbar, so we see the problem occur when we run it? – trincot Aug 26 '22 at 05:53
  • The first issue is that the forward slashes should not be there. Those are delimiters for when you use a regex **literal**, not when it is a string that is parsed as a regex. – trincot Aug 26 '22 at 05:54

0 Answers0