正则表达式 - nodejs中的边界

时间:2017-10-26 19:04:04

标签: node.js regex

我真的不明白正则表达式的界限..

为什么这不起作用?我的猜测是边界不正确,但我无法弄清楚为什么

    var dateInfo = `       This project was

                created on 09/02/2016

                     and last updated 10 days ago.`; 
    var regexString = '/This project was[\s]+ created on ([0-9]{2})\/([0-9]{2})\/([0-9]{4})[\s]+and last updated ([0-9]+) ([a-zA-Z]+)/im';
    var arrMatches = dateInfo.trim().match(regexString);
    console.log(arrMatches);

2 个答案:

答案 0 :(得分:1)

您需要从regexString中删除单引号,使其成为正则表达式字面值:

 var dateInfo = `       This project was

                created on 09/02/2016

                     and last updated 10 days ago.`; 
var regexString = /This project was\s+created on ([0-9]{2})\/([0-9]{2})\/([0-9]{4})\s+and last updated ([0-9]+) ([a-zA-Z]+)/i;
var arrMatches = dateInfo.trim().match(regexString);
console.log(arrMatches);

无需m修饰符。

答案 1 :(得分:0)

我认为字符串dateInfo中的断行(EOL字符)打破了正则表达式。