Regex matches JSON string

I am creating a JSON validator from scratch, but I am completely stuck with a string. My hope was to create a regular expression that would match the following sequence found on JSON.org:

JSON.org Sequence String

So far, my regex is:

/^\"((?=\\)\\(\"|\/|\\|b|f|n|r|t|u[0-9a-f]{4}))*\"$/ 

It matches the criteria with a backslash, the next character, and an empty string. But I'm not sure how to use the UNICODE part.

Is there a regular expression to match any expert with a UNICODE character? or \ or control character? And will it match a new line or horizontal tab?

The final question is that the regex matches the string "\ t" but not "" (four spaces, but the idea should be a tab). Otherwise, I will need to extend the regex with it, which is not a problem, but I assume that the horizontal tab is a UNICODE symbol.

Thanks to Jaeger Kor, I now have the following regex:

 /^\"((?=\\)\\(\"|\/|\\|b|f|n|r|t|u[0-9a-f]{4})|[^\\"]*)*\"$/ 

It seems correct, but is there a way to check for control characters or is it unnecessary as they appear on non-printable characters on the regular -expressions.info? The input for validation is always text from a text field.

Update: The regex looks as if it were needed:

 /^("(((?=\\)\\(["\\\/bfnrt]|u[0-9a-fA-F]{4}))|[^"\\\0-\x1F\x7F]+)*")$/ 
+6
source share
1 answer

For your exact question, create a character class

 # Matches any character that isn't a \ or " /[^\\"]/ 

And then you can just add * to the end to get 0 or an unlimited number of them or, alternatively, 1 or an unlimited number with +

 /[^\\"]*/ 

or

 /[^\\"]+/ 

Also there is the following: https://regex101.com/ under the library tab when searching json

 /(?(DEFINE) # Note that everything is atomic, JSON does not need backtracking if it valid # and this prevents catastrophic backtracking (?<json>(?>\s*(?&object)\s*|\s*(?&array)\s*)) (?<object>(?>\{\s*(?>(?&pair)(?>\s*,\s*(?&pair))*)?\s*\})) (?<pair>(?>(?&STRING)\s*:\s*(?&value))) (?<array>(?>\[\s*(?>(?&value)(?>\s*,\s*(?&value))*)?\s*\])) (?<value>(?>true|false|null|(?&STRING)|(?&NUMBER)|(?&object)|(?&array))) (?<STRING>(?>"(?>\\(?>["\\\/bfnrt]|u[a-fA-F0-9]{4})|[^"\\\0-\x1F\x7F]+)*")) (?<NUMBER>(?>-?(?>0|[1-9][0-9]*)(?>\.[0-9]+)?(?>[eE][+-]?[0-9]+)?)) ) \A(?&json)\z/x 

This should match any valid json, you can also check it on the website above

EDIT:

Regex reference

+7
source

All Articles