模块解析失败:严格模式下的八进制文字,可以在不使用构造函数的情况下使用正则表达式

时间:2018-12-29 01:23:15

标签: regex angular typescript

我正在尝试在Angular6中放置正则表达式:

const regexp = new RegExp('^(?:(?:31(\/|-|\.)(?:0?[13578]|1[02]))\1|(?:(?:29|30)(\/|-|\.)(?:0?[1,3-9]|1[0-2])\2))(?:(?:1[6-9]|[2-9]\d)?\d{2})$|^(?:29(\/|-|\.)0?2\3(?:(?:(?:1[6-9]|[2-9]\d)?(?:0[48]|[2468][048]|[13579][26])|(?:(?:16|[2468][048]|[3579][26])00))))$|^(?:0?[1-9]|1\d|2[0-8])(\/|-|\.)(?:(?:0?[1-9])|(?:1[0-2]))\4(?:(?:1[6-9]|[2-9]\d)?\d{2})$');

但是正如它暗示的那样,它正在抛出

Module parse failed: Octal literal in strict mode 

据我了解,我可以使用\\代替\来避免这种情况。但是有没有任何简便的方法可以在Typescript中完成它。

请提出解决方案。

1 个答案:

答案 0 :(得分:0)

使用.test进行正则表达式对我有帮助,以下是相同的工作代码:

if (/^(?:(?:31(\/|-|\.)(?:0?[13578]|1[02]))\1|(?:(?:29|30)(\/|-|\.)(?:0?[1,3-9]|1[0-2])\2))(?:(?:1[6-9]|[2-9]\d)?\d{2})$|^(?:29(\/|-|\.)0?2\3(?:(?:(?:1[6-9]|[2-9]\d)?(?:0[48]|[2468][048]|[13579][26])|(?:(?:16|[2468][048]|[3579][26])00))))$|^(?:0?[1-9]|1\d|2[0-8])(\/|-|\.)(?:(?:0?[1-9])|(?:1[0-2]))\4(?:(?:1[6-9]|[2-9]\d)?\d{2})$/.test(record['dateofbirth'])) {
            this.validated = false;
            this.validatedtext = this.validatedtext + '\n Date of Birth for record no. ' + i + ' is not correctly formatted';
          }

并且此链接对我有帮助:

https://toddmotto.com/understanding-regular-expression-matching-with-test-match-exec-search-and-split/