这是我的尝试(使用jsfiddle)
function formatDate(date) {
function lz(value,w) {
value = value.toString();
return "0000".slice(4-w + value.length) + value;
}
return [
lz(date.getFullYear(),4),
lz(date.getMonth()+1,2),
lz(date.getDate(),2)
].join('-');
}
//RegExp with support for short (XdYdZ) and long (month_in_text day, year)
var reDate = new RegExp([
'(?:', // Short format
'\\b',
'(\\d{4}|\\d{1,2})', // field.short_value_1
'\\s*([./-])\\s*', // field.short_del_1
'(\\d{1,2})', // field.short_value_2
'\\s*([./-])\\s*', // field.short_del 2
'(\\d{4}|\\d{1,2})', // field.short_value_3
'\\b',
')|(?:', // Long format
'\\b',
'(', // field.long_month
'jan(?:uary)?|',
'feb(?:ruary)?|',
'mar(?:ch)?|',
'apr(?:il)?|',
'may|',
'jun(?:e)?|',
'jul(?:y)?|',
'aug(?:ust)?|',
'sep(?:tember)?|',
'oct(?:ober)?|',
'nov(?:ember)?|',
'dec(?:ember)?',
')',
'\\s+', // required space
'(\\d{1,2})\\b', // field.long_date
'\\s*', // optional space
',?', // optional delimiter
'\\s*', // optinal space
'(\\d{4}|\\d{2})\\b', // field.long_year
')'
].join(''),'i');
//Month names, must be 3 chars lower case.
//Used to convert month name to number.
var monthNames = [
'jan', 'feb', 'mar', 'apr', 'may', 'jun',
'jul','aug', 'sep', 'oct', 'nov', 'dev'
];
var extractDateFromString = function(str) {
var m = str.match(reDate);
var date;
if (m) {
var idx=-1;
//Convert array form regexp result to named variables.
//Makes it so much easier to change the regexp wihout
//changing the rest of the code.
var field = {
all : m[++idx],
short_value_1 : m[++idx],
short_del_1 : m[++idx],
short_value_2 : m[++idx],
short_del_2 : m[++idx],
short_value_3 : m[++idx],
long_month : m[++idx],
long_date : m[++idx],
long_year : m[++idx]
}
//If field.long_month is set it is a date formated with named month
if (field.long_month) {
var month = monthNames.indexOf(
field.long_month.slice(0,3).toLowerCase()
);
// TODO: Add test for sane year
// TODO: Add test for sane month
// TODO: Add test for sane date
date = new Date(field.long_year,month,field.long_date);
} else {
// Short format: value_1 del_1 value_2 del_2 value_3
var year, month, day;
if (field.short_del_1 != field.short_del_2) {
if (
field.short_del_1 === '/' &&
field.short_del_2 === '-'
) {
// DD/MM-YYYY
year = field.short_value_3;
month = field.short_value_2;
day = field.short_value_1;
console.log('DMY',field.all,+year,+month,+day);
} else {
// TODO: Add other formats here.
// If delimiters don't match it isn't a sane date.
console.log('different delimiters');
}
} else {
// assmume YMD if
// (delimiter = '-' and value_3 < 31)
// or (value_1 > 31)
if (
(field.short_del_1 == '-' || field.short_value_1 > 31)
&& (field.short_value_3 < 32)
) {
// YMD
year = field.short_value_1;
month = field.short_value_2;
day = field.short_value_3;
console.log('YMD',field.all,+year,+month,+day);
} else {
// MDY
year = field.short_value_3;
month = field.short_value_1;
day = field.short_value_2;
console.log('MDY',field.all,+year,+month,+day);
}
}
if (year !== undefined) {
year = +year; //convert to number
//Handle years without a century
//year 00-49 = 2000-2049, 50-99 = 1950-1999
if ( year < 100) {
year += year < 50 ? 2000:1900;
}
date = new Date(year,+month-1,+day);
}
}
}
var div = document.createElement('div');
div.className = date ? 'pass' : 'fail';
div.appendChild(document.createTextNode(date?formatDate(date):'NaD'));
div.appendChild(document.createTextNode(' ' + str));
document.body.appendChild(div);
}
for (var i = 0; i < dates.length; i++) {
extractDateFromString(dates[i].name)
}
更新:调整了长格式的正则表达式(添加了 \b)
更新:再次调整正则表达式。不再有 3 位数字字段。(1、2 或 4 个)