HiveMind118
HiveMind118

Reputation: 77

storing email-validation regex string in JSON

I have a regex string for validating email addresses, and I'd like to send it down to my application over json. I get an error from dojo/Json saying:

Uncaught SyntaxError: Unexpected token ]

So I took my JSON file and dropped it into JSONLint and got this slightly more specific error:

Parse error on line 3:
... {        "regex": "^(([^<>()[\]\\.,;:\
----------------------^
Expecting 'STRING', 'NUMBER', 'NULL', 'TRUE', 'FALSE', '{', '['

Here's my json file contents:

{
"Email Address": {
    "regex": "^(([^<>()[\]\\.,;:\s@\"]+(\.[^<>()[\]\\.,;:\s@\"]+)*)|(\".+\"))@((\[[0-9]{1,3}\.[0-9]{1,3}\.[0-9]{1,3}\.[0-9]{1,3}\])|(([a-zA-Z\-0-9]+\.)+[a-zA-Z]{2,}))$"
    }
}

Json.org doesn't seem to say anything about escaping regex characters...and the string itself is escaped properly because I can set a variable to that regex string in a js console.

Anyone know what I can do to get this to work?

Upvotes: 5

Views: 3164

Answers (1)

Lemex
Lemex

Reputation: 3794

This doesnt work because your using " within the strings.

One work around would be to replace every " with something like /'\ then what you parse it again replace every /'\ with " and that will work.

Will look in to a better way but hopefuly this helps in the mean time.

UPDATE:

Every base 64 encode it then decode it: similar to http://decodebase64.com/

And save the base 64 encode regex when your doing stringify and then when you parse decode the the base 64 and store it back..

So basicly

-when wanting to stringify -base64 encode regex -Overwrite normal regex with base 64 -Stringify json

-when wanting to parse JSON - Parse it in - Get base 64 reg ex - Decode it - Replace it

Upvotes: 2

Related Questions