[英]Pattern matching for a string starting with character and ending with digit
Here is a valid string that always start with fixed string SOME_START_FORMAT_
then end with one or more digits. 这是一个有效字符串,始终以固定字符串
SOME_START_FORMAT_
然后以一个或多个数字结尾。 So valid strings are 所以有效的字符串是
SOME_START_FORMAT_1234
SOME_START_FORMAT_12
Invalid strings are 无效的字符串是
SOME_INVALID_FORMAT_1234
SOME_START_FORMAT_
SOME_START_FORMAT_1234_
SOME_START_FORMAT_1234_MORE
I am trying with this regex ^SOME_START_FORMAT_\\d+$
. 我正在尝试使用此正则表达式
^SOME_START_FORMAT_\\d+$
。 What I am doing wrong? 我做错了什么?
you need to check for end of string instead of end of line so nothing after the digits will match (\\z): 您需要检查字符串的末尾而不是行尾,因此数字之后的任何内容都不会匹配(\\ z):
^SOME_START_FORMAT_\d+\z
for multiline this works at regxr.com 对于多行,这在regxr.com上有效
/^SOME_START_FORMAT_\d+$/gm
just have to add the multiline flag. 只需添加多行标志。
Your Regex is fine. 您的正则表达式很好。 I think the fiddle is confusing because it treats all of your input as a single String with several new line characters.
我认为小提琴令人困惑,因为它将所有输入视为带有多个换行符的单个String。
Your pattern works as expected on individual Strings: 您的模式可以在各个字符串上正常工作:
var input = [ "SOME_START_FORMAT_1234", "SOME_START_FORMAT_12", "SOME_INVALID_FORMAT_1234", "SOME_START_FORMAT_", "SOME_START_FORMAT_1234_", "SOME_START_FORMAT_1234_MORE" ]; var pattern = /^SOME_START_FORMAT_\\d+$/; input.forEach(function(s) { var isMatch = s.match(pattern) !== null; document.write(s + ": " + isMatch + "<br>") })
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.