我想从这个字符串中提取时间 "Last Updated on Jul 9 2019, 3:15 pm +08"
<div id="demo"></div>
<script>
var str = "Last Updated on Jul 9 2019, 3:15 pm +08";
var result = str.match(???);
if(result) {
document.getElementById("demo").innerHTML = result;
}
</script>
或者是否可以提取日期和时间,但采用数组形式,如 ['Jul 9 2019','3:15pm']
我刚开始使用正则表达式,不知道如何制定模式。提前致谢!
最佳答案
您可以使用正向后视在字符串中查找“on”,抓取直到 pm/am 的所有内容,并在逗号和空格上拆分,假设格式是一致的:
const str = "Last Updated on Jul 9 2019, 3:15 pm +08"
console.log(str.match(/(?<=on ).*(p|a)m/)[0].split(', '))
Note, the positive lookbehind feature is not compatible with all browsers, so I would recommend using adiga's approach if compatibility is an issue.
关于javascript - 如何使用正则表达式从字符串中提取时间格式,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/56947918/