4

I'm no regular expression guru, so I'm asking for help to come out with a regular expression that would work like this:

var regExp = ???

regExp.exec('\/Date(1330848000000-0800)\/') = [..., '1330848000000', '0800']

// optional gmt
regExp.exec('\/Date(1330848000000)\/') = [..., '1330848000000', null]

regExp.exec('\/Date(1)\/') = [..., '1', null]

// gmt required if - is present
regExp.exec('\/Date(1330848000000-)\/') = null

// escaping backslash is required
regExp.exec('/Date(1330848000000-0800)\/') = null
regExp.exec('\/Date(1330848000000-0800)/') = null

// case sensitive
regExp.exec('\/date(1330848000000-0800)\/') = null

// only numbers allowed
regExp.exec('\/Date(1aaa848000000-0800)\/') = null
regExp.exec('\/Date(1330848000000-0a00)\/') = null

I got stuck pretty early with something as stupid as this:

/\\bla(.*)bla/.exec('\bla123bla') = null // instead of [ ..., '123']

new RegExp('\\\\bla(.*)bla').exec('\bla123bla') = null // instead of [ ..., '123']
3
  • You seen this? stackoverflow.com/questions/1016847/… Commented Jan 30, 2013 at 4:44
  • "Escaping backslash is required" - Actually, with or without the backslash, the 2 strings are exactly the same, since / doesn't need escaping. Commented Jan 30, 2013 at 5:02
  • Yes, I did realize that playing with chrome debugger, but .Net spec requires it, and I'd like to enforce it Commented Jan 30, 2013 at 5:05

3 Answers 3

5

You can use this regex if the string never contains any other numbers apart from the time and the time zone:

/(\d+)(?:-(\d+))?/

Putting into your code:

var regex = /(\d+)(?:-(\d+))?/;
// regex.exec...

If you really need to validate and extract the numbers out of the string:

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

The regex above will check that the string follows the exact format, and also extract the numbers out.

Sign up to request clarification or add additional context in comments.

1 Comment

I got EXACTLY to the same answer! (honest, no cheating), but you came first... So I'll accept your answer
1

The following regex checks for your required constraints:

\\/Date\((\d{13})(-(\d{4}))?\)\\/

It checks for a \ followed by a / followed by the text Date followed by brackets enclosing 13 digits and an optional sequence of - followed by 4 digits, then a required \ and /.

The \\ matches a single \ which requires escaping as it is a special character in regex. Same is in the case of ( and ).

From this, $1 matches the 13 digits inside the brackets and $3 matches the 4 digits if present.

4 Comments

Could you provide the whole javascript sentence? I tried with: new RegExp('\\\\/Date\((\\d{13})(-(\\d{4}))?\)\\\\/').exec('\/Date(1330848000000-0800)\/') and it gives me null
I have only a little knowledge in javascript. Let me try.
Thanks Naveed, I do understand your reasoning, but I seem to be having troubles escaping slashes, I guess...
I think \\\\/Date\\((\\d{13})(-(\\d{4}))?\\)\\\\/ is what you require.
0

I kept playing with regular expressions and finally got it

The escaping slashes are just being ignored by javascript, so this it the solution I came out with (tested on chrome console)

var regExp
undefined

regExp = /^\/Date\((\d+)(?:-(\d+))?\)\/$/
/^\/Date\((\d+)(?:-(\d+))?\)\/$/

regExp.exec('\/Date(1330848000000-0800)\/')
["/Date(1330848000000-0800)/", "1330848000000", "0800"]

regExp.exec('\/Date(1330848000000)\/')
["/Date(1330848000000)/", "1330848000000", undefined]

regExp.exec('\/Date(1)\/')
["/Date(1)/", "1", undefined]

regExp.exec('\/Date(1330848000000-)\/')
null

regExp.exec('/Date(1330848000000-0800)\/')
["/Date(1330848000000-0800)/", "1330848000000", "0800"]

regExp.exec('\/Date(1330848000000-0800)/')
["/Date(1330848000000-0800)/", "1330848000000", "0800"]

regExp.exec('\/date(1330848000000-0800)\/')
null

regExp.exec('\/Date(1aaa848000000-0800)\/')
null

regExp.exec('\/Date(1330848000000-0a00)\/')
null

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.