javascript - 匹配句末的特定单词
问题描述
我有这个正则表达式来检查指定字段中的句子是否必须以概述的字母结尾。这是正则表达式
$scope.pattern = /\b(?:za(?:ZA)?|dtn?|ZA?|DTN)$/;
$scope.error = "must end in za or dtn"
上面的想法是具有模型“模式”的字段必须以 za 或 ZA 或 dtn 或 DTN 结尾。但目前只有当它完全以 za 结束时,才会标记错误
解决方案
Your RegEx looks unnecessarily complex. Try this:
/(za|ZA|dtn|DTN)$/
Or this:
/(za|dtn)$/i
// Uses the 'i' flag to indicate "case-insensitive"
Both of these will match if the input ends with za
, ZA
, dtn
, or DTN
.
If all you're doing is a simple match, it is not necessary to indicate a (?:non-capturing group)
.
推荐阅读
- r - 如何在 R 中的并行工作人员上直接查找/修改对象
- android - 构建错误:无法读取新项目中未定义的属性“长度”
- tensorflow - 尝试使用 Keras 上的回调保存我的模型时,Sequential' 对象没有属性 '_ckpt_saved_epoch' 错误
- c++ - “如何在 C++ 中不使用反向函数递归调用回文数?”
- python - SQLAlchemy:使用动态创建的名称插入表
- python - 在python中初始化orderdict
- mysql - 在 Self 上使用 IN 子查询优化更新查询
- javascript - 如何解决“画布被跨域数据污染”
- java - 从 Tomcat9 运行的 Spark 驱动程序,无法将文件加载到 java 并行流中的 spark 会话中
- javascript - 更改 VSCode 默认格式化程序以将数组项放在新行上?