更新时间:2022-11-14 16:30:11
据我了解,您的模式是这样的:
In my understanding, your pattern is something like this:
<= 31
,则为一天,应将其推入天数组.<= 31
, its a day and should be pushed to days array.注意:如果我的理解是正确的,那么以下解决方案将为您提供帮助.如果没有,请分享不一致之处.
Note: If my understanding is correct, then following solution will help you. If not, please share inconsistencies.
function parseDates(input) {
var parts = processHTMLString(input).split(/[-–,\/\s]/g);
var result = [];
var numRegex = /\d+/;
var final = parts.reduce(function(temp, c) {
if (numRegex.test(c)) {
let num = parseInt(c);
if (temp.year || (temp.month && num < 31)) {
result.push(temp);
temp = {};
}
temp.days = temp.days || []
if (c.indexOf("-") >= 0) {
temp.days = temp.days.concat(c.split("-"));
} else if (num > 31)
temp.year = c;
else
temp.days.push(c)
} else if (c.length < 3 && temp.year) {
temp.suffix = c
} else {
temp.month = c;
}
return temp;
}, {});
result.push(final);
return result;
}
function processHTMLString(str) {
var div = document.createElement("div");
div.innerHTML = str;
// This will process ` ` and any other such value.
return div.textContent;
}
console.log(parseDates('12-5 November 2007 ce 17 May 1954 Ce'));
console.log(parseDates('1 January 1976 CE'));
console.log(parseDates('12 22 March 1965'));