首页 > 解决方案 > 模块解析失败:严格模式下的八进制文字,可以使用正则表达式而不使用构造函数

问题描述

我试图在 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 中完成它。

请提出解决方案。

标签: regexangulartypescript

解决方案


Using .teststatement for regular express 帮助了我,下面是相同的工作代码:

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/


推荐阅读