如何使用 JavaScript/Jquery 从字符串中获取所有日期和时间?
How to fetch all dates and time from string using JavaScript/Jquery?
我正在尝试使用 JS/Jquery 获取所有 DateTime 字段。
目前我正在尝试使用 Regex,但失败得很惨。
输入:
12345 User Name 9/10/2018 11:39:37 AM Valid Entry Place1
12345 User Name 9/10/2018 12:48:43 PM Valid Exit Outside Place1
12345 User Name 9/10/2018 1:00:44 PM Valid Entry Place1
12345 User Name 9/10/2018 2:17:01 PM Valid Exit Outside Place1
12345 User Name 9/10/2018 3:23:36 PM Valid Entry Place1
12345 User Name 9/10/2018 3:25:56 PM Valid Entry Place1
12345 User Name 9/10/2018 6:06:25 PM Valid Exit Outside Place1
12345 User Name 9/10/2018 6:07:55 PM Valid Entry LC
12345 User Name 9/10/2018 6:28:19 PM Valid Exit Outside LC
12345 User Name 9/10/2018 6:30:06 PM Valid Entry Place1
预期输出:
9/10/2018 11:39:37 AM
9/10/2018 12:48:43 PM
9/10/2018 1:00:44 PM
9/10/2018 2:17:01 PM
9/10/2018 3:23:36 PM
9/10/2018 3:25:56 PM
9/10/2018 6:06:25 PM
9/10/2018 6:07:55 PM
9/10/2018 6:28:19 PM
9/10/2018 6:30:06 PM
当前逻辑:
function myFunction() {
var str = document.getElementById("demo").innerHTML;
var txtIdAndName = str.replace(/[0-9]*\s[a-z,\s*]*/ig,"");
var txtStatusAndPlace = txtIdAndName.replace(/[AM,PM]+\s[a-z,\s*]*/ig,"<br/>");
document.getElementById("demo").innerHTML = txtStatusAndPlace;
}
如果我需要更多信息,请告诉我。
可以使用任何逻辑或方法来完成任务。
提前致谢
您可以将这些子字符串与
匹配
s.match(/\b\d{1,2}\/\d{1,2}\/\d{4}\s+\d{1,2}:\d{2}:\d{2}\s*[AP]M\b/g)
详情
\b
- 单词边界
\d{1,2}
- 1 或 2 位数字,
\/
- 斜杠(因为正则表达式文字符号与 /
一起用作定界符而被转义)
\d{1,2}\/\d{4}
- 1 位或 2 位数字,/
然后 4 位数字
\s+
- 1+ 个空格
\d{1,2}:\d{2}:\d{2}
- (时间模式)1 或 2 位数字,:
,2 位数字,:
和另外 2 位数字
\s*
- 0+ 个空格
[AP]
- A
或 P
M
- 一个 M
字符
\b
- 单词边界。
JS 演示:
var s = "12345 User Name 9/10/2018 11:39:37 AM Valid Entry Place1 \n12345 User Name 9/10/2018 12:48:43 PM Valid Exit Outside Place1 \n12345 User Name 9/10/2018 1:00:44 PM Valid Entry Place1 \n12345 User Name 9/10/2018 2:17:01 PM Valid Exit Outside Place1 \n12345 User Name 9/10/2018 3:23:36 PM Valid Entry Place1 \n12345 User Name 9/10/2018 3:25:56 PM Valid Entry Place1 \n12345 User Name 9/10/2018 6:06:25 PM Valid Exit Outside Place1 \n12345 User Name 9/10/2018 6:07:55 PM Valid Entry LC \n12345 User Name 9/10/2018 6:28:19 PM Valid Exit Outside LC \n12345 User Name 9/10/2018 6:30:06 PM Valid Entry Place1";
var rx = /\b\d{1,2}\/\d{1,2}\/\d{4}\s+\d{1,2}:\d{2}:\d{2}\s*[AP]M\b/g;
document.body.innerHTML = "<pre>" + s.match(rx).join("<br/>") + "</pre>";
试试这个正则表达式,
\d+\/\d+\/\d+\s*\d+:\d+:\d+\s*[AP]M
其他用户发布了使用正则表达式的答案。但是,如果字符串的结构不变,则可以使用另一种方式代替正则表达式。
使用javascript .split()
分割字符串,使用.splice()
选择结果的特殊部分。
var str = document.getElementById("demo").innerHTML;
var newStr = str.trim().split("\n").map(function(text){
return text.split(" ").splice(3, 3).join(" ");
}).join("\n");
console.log(newStr);
<div id="demo">
12345 User Name 9/10/2018 11:39:37 AM Valid Entry Place1
12345 User Name 9/10/2018 12:48:43 PM Valid Exit Outside Place1
12345 User Name 9/10/2018 1:00:44 PM Valid Entry Place1
12345 User Name 9/10/2018 2:17:01 PM Valid Exit Outside Place1
12345 User Name 9/10/2018 3:23:36 PM Valid Entry Place1
12345 User Name 9/10/2018 3:25:56 PM Valid Entry Place1
12345 User Name 9/10/2018 6:06:25 PM Valid Exit Outside Place1
12345 User Name 9/10/2018 6:07:55 PM Valid Entry LC
12345 User Name 9/10/2018 6:28:19 PM Valid Exit Outside LC
12345 User Name 9/10/2018 6:30:06 PM Valid Entry Place1
</div>
我正在尝试使用 JS/Jquery 获取所有 DateTime 字段。
目前我正在尝试使用 Regex,但失败得很惨。
输入:
12345 User Name 9/10/2018 11:39:37 AM Valid Entry Place1
12345 User Name 9/10/2018 12:48:43 PM Valid Exit Outside Place1
12345 User Name 9/10/2018 1:00:44 PM Valid Entry Place1
12345 User Name 9/10/2018 2:17:01 PM Valid Exit Outside Place1
12345 User Name 9/10/2018 3:23:36 PM Valid Entry Place1
12345 User Name 9/10/2018 3:25:56 PM Valid Entry Place1
12345 User Name 9/10/2018 6:06:25 PM Valid Exit Outside Place1
12345 User Name 9/10/2018 6:07:55 PM Valid Entry LC
12345 User Name 9/10/2018 6:28:19 PM Valid Exit Outside LC
12345 User Name 9/10/2018 6:30:06 PM Valid Entry Place1
预期输出:
9/10/2018 11:39:37 AM
9/10/2018 12:48:43 PM
9/10/2018 1:00:44 PM
9/10/2018 2:17:01 PM
9/10/2018 3:23:36 PM
9/10/2018 3:25:56 PM
9/10/2018 6:06:25 PM
9/10/2018 6:07:55 PM
9/10/2018 6:28:19 PM
9/10/2018 6:30:06 PM
当前逻辑:
function myFunction() {
var str = document.getElementById("demo").innerHTML;
var txtIdAndName = str.replace(/[0-9]*\s[a-z,\s*]*/ig,"");
var txtStatusAndPlace = txtIdAndName.replace(/[AM,PM]+\s[a-z,\s*]*/ig,"<br/>");
document.getElementById("demo").innerHTML = txtStatusAndPlace;
}
如果我需要更多信息,请告诉我。
可以使用任何逻辑或方法来完成任务。
提前致谢
您可以将这些子字符串与
匹配s.match(/\b\d{1,2}\/\d{1,2}\/\d{4}\s+\d{1,2}:\d{2}:\d{2}\s*[AP]M\b/g)
详情
\b
- 单词边界\d{1,2}
- 1 或 2 位数字,\/
- 斜杠(因为正则表达式文字符号与/
一起用作定界符而被转义)\d{1,2}\/\d{4}
- 1 位或 2 位数字,/
然后 4 位数字\s+
- 1+ 个空格\d{1,2}:\d{2}:\d{2}
- (时间模式)1 或 2 位数字,:
,2 位数字,:
和另外 2 位数字\s*
- 0+ 个空格[AP]
-A
或P
M
- 一个M
字符\b
- 单词边界。
JS 演示:
var s = "12345 User Name 9/10/2018 11:39:37 AM Valid Entry Place1 \n12345 User Name 9/10/2018 12:48:43 PM Valid Exit Outside Place1 \n12345 User Name 9/10/2018 1:00:44 PM Valid Entry Place1 \n12345 User Name 9/10/2018 2:17:01 PM Valid Exit Outside Place1 \n12345 User Name 9/10/2018 3:23:36 PM Valid Entry Place1 \n12345 User Name 9/10/2018 3:25:56 PM Valid Entry Place1 \n12345 User Name 9/10/2018 6:06:25 PM Valid Exit Outside Place1 \n12345 User Name 9/10/2018 6:07:55 PM Valid Entry LC \n12345 User Name 9/10/2018 6:28:19 PM Valid Exit Outside LC \n12345 User Name 9/10/2018 6:30:06 PM Valid Entry Place1";
var rx = /\b\d{1,2}\/\d{1,2}\/\d{4}\s+\d{1,2}:\d{2}:\d{2}\s*[AP]M\b/g;
document.body.innerHTML = "<pre>" + s.match(rx).join("<br/>") + "</pre>";
试试这个正则表达式,
\d+\/\d+\/\d+\s*\d+:\d+:\d+\s*[AP]M
其他用户发布了使用正则表达式的答案。但是,如果字符串的结构不变,则可以使用另一种方式代替正则表达式。
使用javascript .split()
分割字符串,使用.splice()
选择结果的特殊部分。
var str = document.getElementById("demo").innerHTML;
var newStr = str.trim().split("\n").map(function(text){
return text.split(" ").splice(3, 3).join(" ");
}).join("\n");
console.log(newStr);
<div id="demo">
12345 User Name 9/10/2018 11:39:37 AM Valid Entry Place1
12345 User Name 9/10/2018 12:48:43 PM Valid Exit Outside Place1
12345 User Name 9/10/2018 1:00:44 PM Valid Entry Place1
12345 User Name 9/10/2018 2:17:01 PM Valid Exit Outside Place1
12345 User Name 9/10/2018 3:23:36 PM Valid Entry Place1
12345 User Name 9/10/2018 3:25:56 PM Valid Entry Place1
12345 User Name 9/10/2018 6:06:25 PM Valid Exit Outside Place1
12345 User Name 9/10/2018 6:07:55 PM Valid Entry LC
12345 User Name 9/10/2018 6:28:19 PM Valid Exit Outside LC
12345 User Name 9/10/2018 6:30:06 PM Valid Entry Place1
</div>