[英]Check if a string starts with one letter followed by digit(s) and then any characters
How can I make the pattern below return true
in scenarios like these: 如何在以下方案中使下面的模式返回
true
:
m1
, m1a
, M100bc
, s45
, S396xyz
m1
, m1a
, M100bc
, s45
, S396xyz
and false in scenarios like these: 在这样的场景中是假的:
''
, m
, 1
, 1a
, mm
, Mx
, mm1
, SS1b
''
m
, 1
, 1a
, mm
, Mx
, mm1
, SS1b
Pattern to tweak: /^m\\S\\.*/i.test(text)
要调整的模式:/
/^m\\S\\.*/i.test(text)
Right now it takes any number of letters at the start and non-digits right after the first letter 现在它在开头有任意数量的字母,在第一个字母后面有非数字
You may use 你可以用
/^[a-z]\d.*/i
See the regex demo . 请参阅正则表达式演示 。 If the string can have line breaks, replace
.*
with [\\s\\S]*
. 如果字符串可以有换行符,请将
.*
替换为[\\s\\S]*
。
Details 细节
^
- start of string ^
- 字符串的开头 [az]
- an ASCII letter [az]
- 一个ASCII字母 \\d
- a digit \\d
- 一个数字 .*
- any 0+ chars other than line break chars ( [\\s\\S]
will match any chars). .*
- 除了换行符之外的任何0+字符( [\\s\\S]
将匹配任何字符)。 NOTE : The .*
(or [\\s\\S]*
) at the end are only a good idea if you need to use the match values. 注意 :如果需要使用匹配值,最后的
.*
(或[\\s\\S]*
)只是个好主意。 If not, when used with RegExp#test()
, you may omit that part of the pattern. 如果没有,当与
RegExp#test()
,您可以省略该模式的那一部分。
You could just test the first two characters only. 你只能测试前两个字符。
var cases = ['m1', 'm1a', 'M100bc', 's45', 'S396xyz', '', 'm', '1', '1a', 'mm', 'Mx', 'mm1', 'SS1']; console.log(cases.map(s => (/^[az]\\d/i.test(s))));
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.