0

I'm getting strings with values such as /Date(1)/ and /Date(-99999)/. The digits are variable length.

Wouldn't the regex just be this: ^/Date\(d+\)/$

1
  • The purpose of your RegExp is to extract the timestamp, or just to test if the string matchs the pattern ? Commented Feb 3, 2010 at 21:05

3 Answers 3

3

No, your regular expression is missing the optional minus in front of the digits (\d). You also need to escape the / as these are also the delimiter for regular expressions. Try this regular expression:

^/Date\(-?\d+\)/$

Either within the RegExp constructor:

new RegExp("^/Date\\(-?\\d+\\)/$")

Or as literal:

/^\/Date\(-?\d+\)\/$
Sign up to request clarification or add additional context in comments.

Comments

0

Not quite. You're not allowing for the possibility of a leading hyphen, and your slashes are a little wonky.

/^Date\(-?\d+\)$/

Comments

0

Seems that you are getting JSON Date values serialized by ASP .NET, the slashes are included on the strings, to capture the timestamp you can simply match the optional minus sign and any digit character sequence:

var date = "\/Date(1240718400000)\/";
var timeStamp = date.match(/-?\d+/)[0]; // 1240718400000;

Or a more restrictive one:

var timeStamp = date.match(/\/Date\((-?\d+)\)\//)[1]; 

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.