admin管理员组文章数量:1390399
I'm no regular expression guru, so I'm asking for help to e 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']
I'm no regular expression guru, so I'm asking for help to e 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']
Share
Improve this question
asked Jan 30, 2013 at 4:31
opensasopensas
63.8k90 gold badges265 silver badges415 bronze badges
3
- You seen this? stackoverflow./questions/1016847/… – elclanrs 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. – nhahtdh 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 – opensas Commented Jan 30, 2013 at 5:05
3 Answers
Reset to default 5You 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.
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.
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
本文标签: regexjavascript regular expression to parse a net json DatetimeStack Overflow
版权声明:本文标题:regex - javascript regular expression to parse a .net json Datetime - Stack Overflow 内容由网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://www.betaflare.com/web/1744753115a2623297.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
发表评论