如何解析“dd-MMM-yyyy”或“MMM-dd-yyyy”日期字符串使用单一正则表达式? [英] How to parse "dd-MMM-yyyy" or "MMM-dd-yyyy" date string using single regular expression?

查看:324
本文介绍了如何解析“dd-MMM-yyyy”或“MMM-dd-yyyy”日期字符串使用单一正则表达式?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在获取日期字符串,如 dd-MMM-yyyy MMM-dd-yyyy 格式。我想要的是提取 day month year 使用一个正则表达式(不使用任何第三方插件)。可能吗?

解决方案

我会反转月和日位置首先得到所有日期相同的格式:

  function splitDate(input){
return input.replace(
/ ^(\w +) - (\ d +)/,'$ 2- $ 1'
).split(' - ');
}

var parts = splitDate('Feb-12-2012'); // [12,Feb,2012]
var parts = splitDate('12 -Feb-2012'); // [12,Feb,2012]

var day = parts [0];
var month = parts [1];
var year = parts [2];

/ ^(\w +) - (\d +)/ / code>只匹配Mmm-dd-yyyy:

  ^输入
(\w +)一个字符,一次或多次($ 1)
- 一个破折号
(\d +)一个数字,一次或多次($ 2)






进一步



现在你知道如何拆分输入,我想进一步将结果转换为Date对象。首先,让我们打开Chrome的控制台,快速检查输入是否可以解析:

 新日期('12 -Feb- 2012')// Sun Feb 12 2012 ... 
new Date('Feb-12-2012')// Sun 2012年2月12日...

显然,Chrome可以正常完成这项工作。这是非常好的,但是其他实现不一定表现相同的方式,你应该更好地依靠规范。幸运的是,有人已经检查过您:无证支持的Date.parse格式?



总而言之,解析您自己的格式的能力在实现中可能会有所不同。只有一个可靠的格式,即 YYYY-MM-DDTHH:mm:ss.sssZ ,其中每个字母 - T和Z除外 - 代表一个数字



确切地说, MM 预计将是一个数字。如果您想确保您的输入被正确解析,您必须找到一种将月份部分变成数字的方法。让我们尝试一下:

  var months = {
Jan:'01',Feb:'02',Mar: '03',4月:'04',May:'05',Jun:'06',
Jul:'07',Aug:'08',Sep:'09',Oct:'10' 11月:'11',12月:'12'
};
var parts = splitDate('Feb-12-2012'); // [12,Feb,2012]
parts [1] = months [parts [1]]; // parts - > [12,02,2012]

没那么坏,几乎完成实际上,如在规范中提到的那样,解析器只接受日期,如 YYYY-MM-DD ,这是非常容易实现的:

  parts = parts.reverse(); // [2012,02,12] 
parts = parts.join(' - '); //2012-02-12

让我们看看控制台说:

 新日期(部分)// Sun 2012年2月12日... 

完成!






关闭案例



或者,您可以这样做:

  var parts = splitDate('Feb-12- 2012'); // [12,Feb,2012] 
parts [1] = months [parts [1]]; // parts - > [12,02,2012]
var date = new Date(parts [2],parts [1] - 1,parts [0]);

你可以通过将过程隐藏到一个函数中来进一步:

  function parseDate(input){
var map = {
Jan:0,Feb:1,Mar:2,Apr: May:4,Jun:5,
Jul:6,Aug:7,Sep:8,Oct:9,Nov:10,Dec:11
};
return(parseDate = function(input){
input = input.replace(/ ^(\w +) - (\d +)/,'$ 2- $ 1')split(' - ' );
return new Date(input [2],map [input [1]],input [0]);
})(input);
};

使用示例:

  var date = parseDate('12 -Feb-2012'); // Sun Feb 12 2012 ... 
var date = parseDate('Feb-12-2012'); // Sun Feb 12 2012 ...

随意询问详情。


I am getting date string like dd-MMM-yyyy or MMM-dd-yyyy format. All I want is to extract day, month and year using one regular expression (without using any 3rd party plugin). Is it possible?

解决方案

I would invert month and day positions first do get the same format for all dates :

function splitDate(input) {
    return input.replace(
        /^(\w+)-(\d+)/, '$2-$1'
    ).split('-');
}

var parts = splitDate('Feb-12-2012'); // ["12", "Feb", "2012"]
var parts = splitDate('12-Feb-2012'); // ["12", "Feb", "2012"]

var day = parts[0];
var month = parts[1];
var year = parts[2];

/^(\w+)-(\d+)/ matches only "Mmm-dd-yyyy" :

^       beginning of the input
(\w+)   a word character, one or more times ($1)
-       a dash
(\d+)   a digit, one or more times ($2)


To go further

Now that you know how to split the input, I'd like to go further by turning the result into a Date object. Firstly, let's open the Chrome's console to quick check if the input is parseable as is :

new Date('12-Feb-2012') // Sun Feb 12 2012...
new Date('Feb-12-2012') // Sun Feb 12 2012...

Apparently, Chrome can get this job done properly. This is great, but other implementations do not necessarily behave the same way, you should better rely on the specifications. Luckily, someone has already checked them for you : undocumented supported Date.parse format?

To summarize roughly, the ability to parse your own formats is likely to vary across implementations. There is only one reliable format, namely YYYY-MM-DDTHH:mm:ss.sssZ, where every letter - except "T" and "Z" - stands for a digit.

To be precise, MM is expected to be a number. If you want to be sure that your input will be parsed properly, you have to find a way to turn the month part into a number. Let's try something :

var months = {
    Jan: '01', Feb: '02', Mar: '03', Apr: '04', May: '05', Jun: '06',
    Jul: '07', Aug: '08', Sep: '09', Oct: '10', Nov: '11', Dec: '12'
};
var parts = splitDate('Feb-12-2012'); // ["12", "Feb", "2012"]
parts[1] = months[parts[1]]; // parts -> ["12", "02", "2012"]

Not so bad, and the job is almost done. Indeed, as mentionned in the specs, the parser accepts date only formats like YYYY-MM-DD, which is very easy to achieve :

parts = parts.reverse(); // ["2012", "02", "12"]
parts = parts.join('-'); // "2012-02-12"

Let's see what the console says :

new Date(parts) // Sun Feb 12 2012...

Done!


To close the case

Alternatively, you could do this :

var parts = splitDate('Feb-12-2012'); // ["12", "Feb", "2012"]
parts[1] = months[parts[1]]; // parts -> ["12", "02", "2012"]
var date = new Date(parts[2], parts[1] - 1, parts[0]);

You could go even further by hiding the process into a function :

function parseDate(input) {
    var map = {
        Jan: 0, Feb: 1, Mar: 2, Apr: 3, May: 4, Jun: 5,
        Jul: 6, Aug: 7, Sep: 8, Oct: 9, Nov: 10, Dec: 11
    };
    return (parseDate = function (input) {
        input = input.replace(/^(\w+)-(\d+)/, '$2-$1').split('-');
        return new Date(input[2], map[input[1]], input[0]);
    })(input);
};

Usage examples :

var date = parseDate('12-Feb-2012'); // Sun Feb 12 2012...
var date = parseDate('Feb-12-2012'); // Sun Feb 12 2012...

Feel free to ask for details.

这篇关于如何解析“dd-MMM-yyyy”或“MMM-dd-yyyy”日期字符串使用单一正则表达式?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
相关文章
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆