使用Regex从Javascript中的字符串中提取多个日期

时间:2018-04-04 13:58:10

标签: javascript regex date parsing

我是regex的新手,并且看了一遍,虽然我找不到一个有效的正则表达式。字符串中没有空格,日期可以被任何随机文本,非日期字符包围。

示例字符串和预期响应:

EX 1:

From2017-01-01to2017-12-31_custom_EquityPurchaseAgreementMember_custom_FirstPaymentMember_currency_CNY  
var res = ['2017-01-01', '2017-12-31']

EX 2:

From2016-01-01to2016-12-31  
var res = ['2016-01-01', '2016-12-31']

EX 3:

From2017-01-01to2017-12-31_custom_EquityPurchaseAgreementMember_custom_FirstPaymentMember  
var res = ['2017-01-01', '2017-12-31']

EX 4:

AsOf2017-12-31  
var res = ['2017-12-31']

我试过这个正则表达式(等等),但没有用: /\b\d{4}-\d{2}-\d{2}\b/g

我正在使用此工具进行测试: http://rubular.com/r/bce4IHyCjW

4 个答案:

答案 0 :(得分:2)

您与日期不匹配,因为单词边界\b不匹配,例如m2

尝试不带边界\b

\d{4}\-\d{2}\-\d{2}

const strings = [
  "From2017-01-01to2017-12-31_custom_EquityPurchaseAgreementMember_custom_FirstPaymentMember_currency_CNY",
  "From2016-01-01to2016-12-31",
  "From2017-01-01to2017-12-31_custom_EquityPurchaseAgreementMember_custom_FirstPaymentMe",
  "AsOf2017-12-31"
];
let pattern = /\d{4}\-\d{2}\-\d{2}/g;
strings.forEach((s) => {
  console.log(s.match(pattern));
});

答案 1 :(得分:1)

删除字边界\b

var regex =  /\d{4}-\d{2}-\d{2}/g

<强>演示

var str1 = "2017-01-01to2017-12-31_custom_EquityPurchaseAgreementMember_custom_FirstPaymentMember_currency_CNY";
var str2 = "From2016-01-01to2016-12-31";
var str3 = "From2017-01-01to2017-12-31_custom_EquityPurchaseAgreementMember_custom_FirstPaymentMember";
var str4 = "AsOf2017-12-31";

var regex =  /\d{4}-\d{2}-\d{2}/g

console.log( str1.match(regex) );
console.log( str2.match(regex) );
console.log( str3.match(regex) );
console.log( str4.match(regex) );

答案 2 :(得分:0)

您可以使用

/(?:^|\D)(\d{4}-\d{2}-\d{2})(?!\d)/g

提取组1.请参阅regex demo

此处,(?:^|\D)匹配字符串的开头(^)或(|)除数字(\D)以外的任何字符。如果当前位置右侧有一个数字,则(?!\d)是一个负向前瞻,但未达到匹配。

JS Demo:

&#13;
&#13;
var strs = ["From2017-01-01to2017-12-31_custom_EquityPurchaseAgreementMember_custom_FirstPaymentMember_currency_CNY","From2016-01-01to2016-12-31", "From2017-01-01to2017-12-31_custom_EquityPurchaseAgreementMember_custom_FirstPaymentMember", "AsOf2017-12-31" ];
var rx = /(?:^|\D)(\d{4}-\d{2}-\d{2})(?!\d)/g;
for (var s of strs) {
  console.log(s);
  var res=[], m;
  while (m=rx.exec(s)) {
     res.push(m[1]);
  }
  console.log(res);
}
&#13;
&#13;
&#13;

浏览器支持ECMA 2018标准(支持lookbehinds),

/(?<!\d)\d{4}-\d{2}-\d{2}(?!\d)/g

another regex demo。如果当前位置的左侧有一个数字,则(?<!\d)是一个负面的后视,但是匹配失败。

JS Demo:

&#13;
&#13;
var strs = ["From2017-01-01to2017-12-31_custom_EquityPurchaseAgreementMember_custom_FirstPaymentMember_currency_CNY","From2016-01-01to2016-12-31", "From2017-01-01to2017-12-31_custom_EquityPurchaseAgreementMember_custom_FirstPaymentMember", "AsOf2017-12-31" ];
var rx = /(?<!\d)\d{4}-\d{2}-\d{2}(?!\d)/g;
for (var s of strs) {
  console.log(s, "=>", s.match(rx));
}
&#13;
&#13;
&#13;

答案 3 :(得分:0)

日期值可以是1-1-201501-25-2015因此,为了使您的正则表达式更加通用,您应该使用此正则表达式

var regex = /([0-9]+-[0-9]+-[0-9]+)/g

您可以使用此网站测试您的正则表达式,它非常棒,并且可以为某些语言生成代码regex101