简体   繁体   English

检查一个字符串是以一个字母开头,后跟数字,然后是任何字符

[英]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 m1m1aM100bcs45S396xyz

and false in scenarios like these: 在这样的场景中是假的:

'' , m , 1 , 1a , mm , Mx , mm1 , SS1b '' m11ammMxmm1SS1b

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.

相关问题 检查文本是否以字符后跟数字开头 - Check if text starts with characters followed by numbers 正则表达式,至少一个字母,后跟一个空格,后跟至少一个数字 - Regular expression for at least one letter followed by a white space, followed by at least one digit 如何检查字符串是否有星号后跟四位数字? - how to check whether a string have stars followed by four digit number? 如何检查我的字符串是否以 WA 后跟 9 位数字结尾 - How do I check if my string ends with WA followed by 9 digit 以字母开头的正则表达式包含一个大写/一个小写字母,一个数字,没有特殊字符和最少8个字符 - Regex that starts with letter, contains one uppercase/one lowercase letter, one number and no special characters & min 8 characters 检查字符串是否以数组中的任何字符串开头 - Check if a string starts with any of the strings in an array javascript 检查字符串是否以某些字符开头并删除这些字符 - javascript check if string starts with some characters and remove those characters 检查字符串是否包含任何字母(Javascript/jquery) - Check if string contains any letter (Javascript/jquery) 如何检查是否以字母开头的数字 - How to check if a number that starts with a letter 如何创建仅以空格后的字符串中的第一个字母或任何其他字母开头的自动完成功能 - How to create autocomplete which only starts with either first letter or any other letter in the string starting after space
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM