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']
opensas
  • 60,462
  • 79
  • 252
  • 386
  • You seen this? http://stackoverflow.com/questions/1016847/converting-net-datetime-to-json – elclanrs Jan 30 '13 at 04:44
  • "Escaping backslash is required" - Actually, with or without the backslash, the 2 strings are exactly the same, since `/` doesn't need escaping. – nhahtdh Jan 30 '13 at 05:02
  • Yes, I did realize that playing with chrome debugger, but .Net spec requires it, and I'd like to enforce it – opensas Jan 30 '13 at 05:05

3 Answers3

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.

nhahtdh
  • 55,989
  • 15
  • 126
  • 162
  • I got EXACTLY to the same answer! (honest, no cheating), but you came first... So I'll accept your answer – opensas Jan 30 '13 at 06:27
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.

Naveed S
  • 5,106
  • 4
  • 34
  • 52
  • 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 – opensas Jan 30 '13 at 04:59
  • I have only a little knowledge in javascript. Let me try. – Naveed S Jan 30 '13 at 05:01
  • Thanks Naveed, I do understand your reasoning, but I seem to be having troubles escaping slashes, I guess... – opensas Jan 30 '13 at 05:04
  • 1
    I think `\\\\/Date\\((\\d{13})(-(\\d{4}))?\\)\\\\/` is what you require. – Naveed S Jan 30 '13 at 05:09
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
opensas
  • 60,462
  • 79
  • 252
  • 386