I have an arbitrary (E)JSON that gets created and sent over the wire from client to server in my Meteor app. It uses RegExp
objects to zero-in on results:
# on the client
selector =
"roles.user": { "$ne": null }
"profile.email": /^admin@/gi
All is dandy fine on the client side, but if I pass this to the server via Meteor.call
or Meteor.subscribe
, the resulting (E)JSON takes this form:
# on the server
selector =
"roles.user": { "$ne": null }
"profile.email": {}
...and somewhere an engineer dies a little on the inside.
There are plenty of resources on the Web explaining why RegEx is unserializable via JSON.stringify
/JSON.parse
or the equivalent EJSON
methods.
I'm not convinced RegEx serialization is impossible. So how can it be done?
There is a far simpler solution: stringify your RegExp via
.toString()
, send it to the server and then parse it back to RegExp.After reviewing this HowTo and the Meteor EJSON Docs, we may serialize RegEx using the
EJSON.addType
method.Extend RegExp - Provide RegExp with the methods
EJSON.addType
requires for implementation.Call EJSON.addType - Do this anywhere. It's best to make it available to client AND server though. This is going to deserialize the object defined in
toJSONValue
above.Test In Your Console - Don't take my word for it. See for yourself.
And there you have a RegExp being serialized and parsed on client and server, able to be passed in over the wire, saved in a Session, and even possibly stored in a Collection of queries!
EDIT to addess IE10+ Error: Assignment to read-only properties is not allowed in strict mode Courtesy of @Tim Fletcher in the comments