首页 > 解决方案 > 匹配句末的特定单词

问题描述

我有这个正则表达式来检查指定字段中的句子是否必须以概述的字母结尾。这是正则表达式

$scope.pattern = /\b(?:za(?:ZA)?|dtn?|ZA?|DTN)$/;
$scope.error = "must end in za or dtn"

上面的想法是具有模型“模式”的字段必须以 za 或 ZA 或 dtn 或 DTN 结尾。但目前只有当它完全以 za 结束时,才会标记错误

标签: javascriptangularjsregex

解决方案


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).


推荐阅读