admin管理员组文章数量:1278653
How can I make the pattern below return true
in scenarios like these:
m1
, m1a
, M100bc
, s45
, S396xyz
and false in scenarios like these:
''
, m
, 1
, 1a
, mm
, Mx
, mm1
, SS1b
Pattern to tweak: /^m\S\.*/i.test(text)
Right now it takes any number of letters at the start and non-digits right after the first letter
How can I make the pattern below return true
in scenarios like these:
m1
, m1a
, M100bc
, s45
, S396xyz
and false in scenarios like these:
''
, m
, 1
, 1a
, mm
, Mx
, mm1
, SS1b
Pattern to tweak: /^m\S\.*/i.test(text)
Right now it takes any number of letters at the start and non-digits right after the first letter
Share Improve this question asked Jun 30, 2017 at 19:04 SeaBassSeaBass 1,7645 gold badges24 silver badges55 bronze badges2 Answers
Reset to default 6You may use
/^[a-z]\d.*/i
See the regex demo. If the string can have line breaks, replace .*
with [\s\S]*
.
Details
^
- start of string[a-z]
- an ASCII letter\d
- a digit.*
- any 0+ chars other than line break chars ([\s\S]
will match any chars).
NOTE: The .*
(or [\s\S]*
) at the end are only a good idea if you need to use the match values. If not, when used with RegExp#test()
, you may omit that part of the pattern.
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 => (/^[a-z]\d/i.test(s))));
本文标签:
版权声明:本文标题:javascript - Check if a string starts with one letter followed by digit(s) and then any characters - Stack Overflow 内容由网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://www.betaflare.com/web/1741294820a2370755.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
发表评论