[英]RegEx to match exactly one occurrence of character at the end of string in Javascript
I have a string and want to match pattern which has either s or m at the end of string having only one occurrence.我有一个字符串,并且想要匹配在字符串末尾只有一次出现的 s 或 m 的模式。 I haven't used regex much and unable to find any answers.
我没有太多使用正则表达式,也找不到任何答案。
Eg If the string is 121ss.例如,如果字符串是 121ss。 It should return false.
它应该返回假。 121s and 121m should return true.
121s 和 121m 应该返回 true。 121ms should also return false.
121ms 也应该返回 false。 It is also case sensitive so 121M or 121H won't do.
它也区分大小写,因此 121M 或 121H 不会。
The pattern I tried using is /[mh]{1}$/
我尝试使用的模式是
/[mh]{1}$/
If "m"
and "s"
must be preceded by at least one or more numbers \d+
use:如果
"m"
和"s"
必须以至少一个或多个数字开头\d+
使用:
/\d+[ms]$/
Demo on Regex101 Regex101 上的演示
1m // matches
123m // matches
1s // matches
123s // matches
121ss
121ms
1M
1S
123MS
xyzn
xyzs
PS: if you already use [ms]
(meaning "m or s" ) then the {1}
is unnecessary since it matches only one option. PS:如果您已经使用
[ms]
(意思是"m or s" ),那么{1}
是不必要的,因为它只匹配一个选项。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.