javascript 从日期正则表达式中提取月、日和年
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/26934703/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Extract month, day and year from date regex
提问by Athapali
I have a date in "mm/dd/yyyy" format. But, depending on what day or month, there is no guarantee that the "mm/dd" part will necessarily have two digits. So the dates may be:
我有一个“mm/dd/yyyy”格式的日期。但是,根据日期或月份,不能保证“mm/dd”部分一定有两位数。所以日期可能是:
11/12/2014
or
或者
6/12/2014
or
或者
11/5/2014
In all of the above situations I will need to be able to extract each value for mm
, dd
, and yyyy
so I can do something like this for a calculation:
在所有上述情况我需要能够提取每个值mm
,dd
和yyyy
这样我可以计算做这样的事情:
Month = regex that returns month part.
Day = regex that returns day part
Year = regex that returns 4 digits year part.
This \d{2}/\d{2}/\d{2}
matches the whole thing.
这\d{2}/\d{2}/\d{2}
与整个事情相匹配。
回答by Micha? Miszczyszyn
Regexp Solution
正则表达式解决方案
The proper regex matching all of your dates is: \d{1,2}/\d{1,2}/\d{4}
. Notation
{n,m}
is called limiting repetitionand in this case \d{n,m}
means "between n and m digits (inclusive)". You can read more about that here: Limiting Repetition.
正确的正则表达式匹配所有的日期是:\d{1,2}/\d{1,2}/\d{4}
。符号
{n,m}
称为限制重复,在这种情况下\d{n,m}
意味着“在 n 和 m 位数之间(包括)”。您可以在此处阅读更多相关信息:限制重复。
If you want to create matching groups, use "(" and ")" (parentheses). Addiitonally, in JavaScript, you have to to escape /
with \
and thus your regexp is: /(\d{1,2})\/(\d{1,2})\/(\d{4})/
如果要创建匹配组,请使用“(”和“)”(括号)。另外,在 JavaScript 中,你必须逃避/
,\
因此你的正则表达式是:/(\d{1,2})\/(\d{1,2})\/(\d{4})/
You can use exec
function of the regexp to match strings against it. It returns an array containing the matched text as the first element and then one item for each of the matched groups.
您可以使用正exec
则表达式的函数来匹配字符串。它返回一个数组,其中包含匹配的文本作为第一个元素,然后每个匹配的组都有一个项目。
You can find out more on MDN RegExp.prototype.execor see this example below:
您可以在 MDN RegExp.prototype.exec上找到更多信息或查看下面的示例:
const datePattern = /(\d{1,2})\/(\d{1,2})\/(\d{4})/;
const date = datePattern.exec("11/12/2014"); // ["11/12/2014", "11", "12", "2014"]
Note:If an invalid string is passed (not matching the regular expression) exec
function will return null
. It's an easy way to do brief validation of users' input or data.
注意:如果传递了无效字符串(与正则表达式不匹配),exec
函数将返回null
. 这是对用户的输入或数据进行简要验证的一种简单方法。
Alternative solution
替代方案
Alternatively you can simply split the strings:
或者,您可以简单地拆分字符串:
"11/12/2014".split('/'); // ["11", "12", "2014"]
But this will work regardless of what the string actually contains (it might contain string "ba/tm/an"
and split will still return an array with three elements). For this reason, Regexp is probably a better fit if you want to validate the dates.
但是无论字符串实际包含什么,这都可以工作(它可能包含字符串,"ba/tm/an"
而 split 仍将返回一个包含三个元素的数组)。因此,如果您想验证日期,Regexp 可能更合适。
回答by Johan Karlsson
Try this:
试试这个:
var str = "11/6/2014";
var match = str.match(/(\d{1,2})\/(\d{1,2})\/(\d{4})/);
console.log(match[1]); // => "11"
console.log(match[2]); // => "6"
console.log(match[3]); // => "2014"
alternatively you can create a date object:
或者,您可以创建一个日期对象:
var date = new Date("11/6/2014");
var year = date.getFullYear();
var month = date.getMonth()+1;
var day = date.getDate();
console.log(year); // => 2014
console.log(month); // => 11
console.log(day); // => 6
回答by Clint Powell
You don't need regex to extract the pieces, just use split
. I used regex to allow forward or backward slashes.
您不需要正则表达式来提取碎片,只需使用split
. 我使用正则表达式来允许正斜杠或反斜杠。
var date = "1/10/2014"
, dateParts = date.split(/\|\//)
, month = dateParts[0]
, day = dateParts[1]
, year = dateParts[2];