I am building a JSON validator from scratch, but I am quite stuck with the string part. My hope was building a regex which would match the following sequence found on JSON.org:
My regex so far is:
/^"((?=\)\("|/|\|b|f|n|r|t|u[0-9a-f]{4}))*"$/
It does match the criteria with a backslash following by a character and an empty string. But I'm not sure how to use the UNICODE part.
Is there a regex to match any UNICODE character expert " or or control character? And will it match a newline or horizontal tab?
The last question is because the regex match the string "", but not " " (four spaces, but the idea is to be a tab). Otherwise I will need to expand the regex with it, which is not a problem, but my guess is the horizontal tab is a UNICODE character.
Thanks to Jaeger Kor, I now have the following regex:
/^"((?=\)\("|/|\|b|f|n|r|t|u[0-9a-f]{4})|[^"]*)*"$/
It appears to be correct, but is there any way to check for control characters or is this unneeded as they appear on the non-printable characters on regular-expressions.info? The input to validate is always text from a textarea.
Update: the regex is as following in case anyone needs it:
/^("(((?=\)\(["\/bfnrt]|u[0-9a-fA-F]{4}))|[^"\-x1Fx7F]+)*")$/
See Question&Answers more detail:
os 与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…