使用字符串文字正则表达式变量创建另一个正则表达式模式

时间:2019-01-01 05:22:59

标签: javascript regex

我想从文本中获取日期并将其转换为秒。日期的格式为 Day Month Year Hour 。我正在使用正则表达式来匹配模式,但是正则表达式很长且不可读。因此,我为每个正则表达式创建了一个变量。现在,我想如何使用这些变量创建另一个匹配模式。我正在使用javascript,这是我的代码

```javascript

/ detect the day
let day = /(lundi|mardi|mercredi|jeudi|vendredi|samedi|dimanche)/i;

// detect the num of the day
let numDay = /(0[1-9]|1[0-9]|2[0-9]|3[0-1])/;
//detect the month
let month = /(janvier|fevrier|mars|avril|mai|juin|juillet|aout|septembre|octobre|novembre|decembre)/i;
// detecter the year
let year = /(197[0-9]|20[0-9][0-9])/;
// detecter the hours in min and sec
let hour = /([0-1][0-9]|2[0-3]):[0-5][0-9]:[0-5][0-9]/;

let validFormat = `${day} ${numDay} ${month} ${year} ${hour}`;

let expression = "Mardi 12 Novembre 2013 12:02:21";

let found = expression.match(validFormat);

console.log(found);

```

0 个答案:

没有答案
相关问题