[英]How to remove timestamp using regex from a date string in javascript
New to regex. 正则表达式的新功能。
Need regular expression to match strings like T17:44:24Z
from parent strings like 2015-12-22T17:44:24Z
using javascript regex. 需要正则表达式以使用javascript正则表达式从诸如2015-12-22T17:44:24Z
类的父字符串中匹配诸如T17:44:24Z
字符串。
Once match is found will replace it with null or empty space. 找到匹配项后,将其替换为null或空白。
To be specific i am trying to remove the time stamp from date string and obtain only date part. 具体来说,我试图从日期字符串中删除时间戳,只获取日期部分。
Please help me in this 请帮我
You can use a simple regex like this: 您可以使用一个简单的正则表达式,如下所示:
T\d{2}:\d{2}:\d{2}Z
or
T(?:\d{2}:){2}\d{2}Z
In case your T
and Z
are dynamic the, you can use: 如果您的T
和Z
是动态的,则可以使用:
[A-Z]\d{2}:\d{2}:\d{2}[A-Z]
Code 码
var re = /T\d{2}:\d{2}:\d{2}Z/g;
var str = '2015-12-22T17:44:24Z';
var subst = '';
var result = str.replace(re, subst);
You don't need regex on this. 您不需要正则表达式。 You just split the string by T
and get the second element from array, which would be 17:44:24Z
in your case. 您只需将字符串除以T
,然后从数组中获取第二个元素,在您的情况下为17:44:24Z
。
var date = '2015-12-22T17:44:24Z';
var result = date.split('T')[1];
If you also want to preserve T, you can just prepend it to the result: 如果您还想保留T,则可以将其放在结果的前面:
var result = 'T' + date.split('T')[1]
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.