Javascript-如何从ajax responsetext中提取日期时间

时间:2018-11-11 02:52:41

标签: javascript

Javascript-如何从此Ajax响应文本中提取日期时间:

This text was brought to you by AJAX 
Your request was received at:
Sunday 11 November 2018, 02:40:00 am

1 个答案:

答案 0 :(得分:0)

我将使用正则表达式来完成此操作。正则表达式:

/\w{3,6}day.*(?:am|pm)$/i

首先匹配3或4个Word chars,然后匹配'day',然后匹配任意数量的any char,直到达到'am''pm'为止,然后字符串的结尾。它使用不区分大小写的匹配。

使用方法:

var text = 'This text was brought to you by AJAX' 
    + 'Your request was received at:'
    + 'Sunday 11 November 2018, 02:40:00 am'

var dateStr = text.match(/\w{3,4}day.*(?:am|pm)$/i);
var date = new Date(dateStr);

修改

将量词更改为{3,6},使其也与Wednesday匹配。