How do I validate that a string/JSON value is in the format "\/Date(1239018869048)\/"?
I’m iterating through a JSON object, I know I may have a serialized JSON date string but because the JSON object is dynamic, I don’t know which property is the serialized JSON date.
So I want to know if a JSON property value will validate to the format of a JSON serialized date.
Update #1
It would be nice to check if it’s an instance of a string before you use the regular expression because an integer will throw an exception. This is in addition to the answer @vzwick gave. Thanks once again.
json_obj = { 'foo' : 'bar', 'baz' : '/Date(1239018869048)/' }
pattern = /^\/Date\((\d*)\)\/$/;
for(e in json_obj) {
if (json_obj[e].constructor === String) {
if (json_obj[e].match(pattern)) {
// date found
}
}
}
Update #2
After tryout different values, i discovered that we have negative numbers. So the pattern can
now look like;
pattern = /^\/Date\((-?\d*)\)\/$/;
Here you go, mate: