我正在尝试使用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;
}
请告知我是否需要进一步的信息。
可以使用任何逻辑或方法来完成任务。
预先感谢
答案 0 :(得分:2)
您可以将这些子字符串与
匹配s.match(/\b\d{1,2}\/\d{1,2}\/\d{4}\s+\d{1,2}:\d{2}:\d{2}\s*[AP]M\b/g)
请参见regex demo
详细信息
\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>";
答案 1 :(得分:0)
答案 2 :(得分:0)
其他用户发布了使用正则表达式的答案。但是,如果字符串的结构是恒定的,则可以使用另一种方法来代替正则表达式。
使用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>