regex - 模块解析失败:严格模式下的八进制文字,可以使用正则表达式而不使用构造函数
问题描述
我试图在 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 中完成它。
请提出解决方案。
解决方案
Using .test
statement 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';
}
这个链接帮助了我:
推荐阅读
- c++ - 尝试修复一个字符串函数,该函数接受一个字符串并通过替换一些单词来更改它
- c# - 如何从另一个类更改项目的值?
- salesforce - Apex 错误 - 尝试访问记录值时变量不存在
- python - pdb 无法使用 from currentdir import mod
- c# - 如何计算有多少客户在 SQL 中购买了产品
- css - 如何修复自定义字体的 CORS 错误?
- python - 从给定范围内的张量中删除值
- c# - 生成顺序很重要的列表的powerset
- javascript - 部署到 HTTPS 后无法访问 req.user
- ios - SDWebImage 使用 Swift 在 CellView 中将图像设置为 UIButton