javascript 正则表达式,用于解析 .NET json Datetime
javascript regular expression to parse a .net json Datetime
我不是正则表达式大师,所以我请求帮助来提出一个像这样工作的正则表达式:
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
我很早就被这样愚蠢的事情困住了:
/''bla(.*)bla/.exec(''bla123bla') = null // instead of [ ..., '123']
new RegExp('''''bla(.*)bla').exec(''bla123bla') = null // instead of [ ..., '123']
如果字符串从不包含除时间和时区之外的任何其他数字,则可以使用此正则表达式:
/('d+)(?:-('d+))?/
放入您的代码中:
var regex = /('d+)(?:-('d+))?/;
// regex.exec...
如果您确实需要验证并从字符串中提取数字:
/^'/Date'(('d+)(?:-('d+))?')'/$/
上面的正则表达式将检查字符串是否遵循确切的格式,并将数字提取出来。
以下正则表达式检查所需的约束:
''/Date'(('d{13})(-('d{4}))?')''/
它检查'
后跟/
,后跟文本Date
后跟括号(括起 13 位数字)和可选的-
序列,后跟 4 位数字,然后检查所需的'
和/
。
''
匹配需要转义的单个'
,因为它是正则表达式中的特殊字符。(
和)
的情况也是如此。
由此,$1 匹配括号内的 13 位数字,$3 匹配 4 位数字(如果存在)。
我一直在玩正则表达式,终于明白了
转义斜杠只是被javascript忽略了,所以这是我提出的解决方案(在chrome控制台上测试)
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
相关文章:
- 如何将JSON结果设置为'src'共'img'以在ASP.NET MVC4中显示图像
- 使用javascript在MVC视图中解析.net JSON字符串
- ASP.NET WebApi DateTimeOffset 序列化为 Json/JavaScript (angular2
- 将列表<字符串>转换为客户端 ASP.NET MVC 上的 Json 数组
- 意外结果,在 ASP.Net 中解析 JSON 对象的数组
- 在设置用户后,从 JavaScript JSON 请求中获取 Web 服务 (asmx) ASP.NET Windows
- 如何将 JSON.NET 日期时间格式转换为javascript日期和时间
- 当Json Length过长时,对ASP.NET MVC Controller的Ajax调用返回404
- $http-get方法即使使用AngularJS中.Net WebApi的有效json也会失败
- .NET服务中的JSON反序列化-can'不能解决这个问题
- 用asp.net mvc将json数据渲染到网格中
- asp.net mvc操作方法发布除json数据之外的数据
- 使用ASP.Net MVC Web Api将JSON数据导出到Excel工作表
- ASP.NET MVC-自动取消序列化JSON Fetch POST
- VB.Net JSON 序列化格式
- ASP.Net-Json字符串无效
- . net JSON.decode()对于大数组失败
- 防止ASP.Net JSON从在十进制属性中删除尾随0
- javascript 正则表达式,用于解析 .NET json Datetime
- 日期转换.net JSON到ISO