Regexp中的命名组从字符串转换为新的RegExp结构

时间:2019-05-10 16:16:49

标签: javascript node.js regex

我正在尝试从一系列较小的Regexes(以字符串或原始形式)构建Regexp。

我正在使用Node v10.15.0。

这里分别是我的三个组成部分

每月匹配项:/\b(?<month>\bjan(?:uary)?\b|\bfeb(?:ruary)?\b|\bmar(?:ch)?\b|\bapr(?:il)?\b|\bmay\b|\bjun(?:e)?\b|\bjul(?:y)?\b|\baug(?:ust)?\b|\bsep(?:tember)?\b|\boct(?:ober)?\b|\bnov(?:ember)?\b|\bdec(?:ember)?\b)/i

每日匹配项:/(?<day>\d{1,2})/i

年度匹配者:/(?<year>20\d\d)/i

我正在尝试从每一个创建一个正则表达式,如下所示:

new RegExp(/\b(?<month>\bjan(?:uary)?\b|\bfeb(?:ruary)?\b|\bmar(?:ch)?\b|\bapr(?:il)?\b|\bmay\b|\bjun(?:e)?\b|\bjul(?:y)?\b|\baug(?:ust)?\b|\bsep(?:tember)?\b|\boct(?:ober)?\b|\bnov(?:ember)?\b|\bdec(?:ember)?\b) (?<day>\d{1,2}), (?<year>20\d\d)/i);

这将与“ 2018年4月14日”,“ 2019年6月25日”等匹配。

我已经尝试了许多构建:

  • new RegExp(/my-pattern/i)
  • new RegExp('my-pattern' + 'my-other-pattern, 'i')
  • new RegExp(new RegExp('my-pattern', 'i') + new RegExp('other-pattern', 'i')(这个人觉得很傻)。

我注意到一个奇怪的效果是,当我尝试构建字符串时。通过加法,构造函数将剪切输出-看看下面如何更改“ month”命名组:

var z = new RegExp('\b(?<month>\bjan(?:uary)?\b|\bfeb(?:ruary)?\b|\bmar(?:ch)?\b|\bapr(?:il)?\b|\bmay\b|\bjun(?:e)?\b|\bjul(?:y)?\b|\baug(?:ust)?\b|\b
sep(?:tember)?\b|\boct(?:ober)?\b|\bnov(?:ember)?\b|\bdec(?:ember)?\b)' + '(?<day>\d{1,2})', 'i');
undefined

>>> (?<monthjan(?:uary)feb(?:ruary)mar(?:ch)apr(?:il)majun(?:e)jul(?:y)aug(?:ust)sep(?:tember)oct(?:ober)nov(?:ember)dec(?:ember))(?<day>d{1,2})/i

有人可以建议最好的方法吗?否则,我可能会以非常冗长的模式一遍又一遍地声明月/日/年匹配项。

谢谢

1 个答案:

答案 0 :(得分:0)

This expression可以帮助您匹配所需的日期字符串。

((1[0-2]|0?[1-9])\/(3[01]|[12][0-9]|0?[1-9])\/(?:[0-9]{2})?[0-9]{2})|(Jan(uary)?|Feb(ruary)?|Mar(ch)?|Apr(il)?|May|Jun(e)?|Jul(y)?|Aug(ust)?|Sep(tember)?|Oct(ober)?|Nov(ember)?|Dec(ember)?)\s+\d{1,2},\s+\d{4}

您可以根据需要简化它并缩小边界。

enter image description here

RegEx描述图

该图将表达式可视化,并且如果需要,您可以在此link中测试其他表达式:

enter image description here

JavaScript测试

const regex = /((1[0-2]|0?[1-9])\/(3[01]|[12][0-9]|0?[1-9])\/(?:[0-9]{2})?[0-9]{2})|(Jan(uary)?|Feb(ruary)?|Mar(ch)?|Apr(il)?|May|Jun(e)?|Jul(y)?|Aug(ust)?|Sep(tember)?|Oct(ober)?|Nov(ember)?|Dec(ember)?)\s+\d{1,2},\s+\d{4}/gm;
const str = `Apr 14, 2018`;
let m;

while ((m = regex.exec(str)) !== null) {
    // This is necessary to avoid infinite loops with zero-width matches
    if (m.index === regex.lastIndex) {
        regex.lastIndex++;
    }
    
    // The result can be accessed through the `m`-variable.
    m.forEach((match, groupIndex) => {
        console.log(`Found match, group ${groupIndex}: ${match}`);
    });
}

基本性能测试

此JavaScript代码段返回100万次for循环以提高性能。

const repeat = 1;
const start = Date.now();

for (var i = repeat; i >= 0; i--) {
	const string = 'Apr 14, 2018';
	const regex = /(((1[0-2]|0?[1-9])\/(3[01]|[12][0-9]|0?[1-9])\/(?:[0-9]{2})?[0-9]{2})|(Jan(uary)?|Feb(ruary)?|Mar(ch)?|Apr(il)?|May|Jun(e)?|Jul(y)?|Aug(ust)?|Sep(tember)?|Oct(ober)?|Nov(ember)?|Dec(ember)?)\s+\d{1,2},\s+\d{4})/gm;
	var match = string.replace(regex, "Group #1: $1");
}

const end = Date.now() - start;
console.log("YAAAY! \"" + match + "\" is a match  ");
console.log(end / 1000 + " is the runtime of " + repeat + " times benchmark test.  ");