简体   繁体   English

Javascript正则表达式匹配至少一个以数字开头的单词跨越多个单词但至少有两个单词

[英]Javascript regex to match atleast one word starting with number across multiple words but with atleast two words

I am using Javascript Regex.我正在使用 Javascript 正则表达式。 I have an input field which should pass only the follwing condition我有一个输入字段,它应该只通过以下条件

  • Value should have only alphabets and numbers值应该只有字母和数字
  • Value Should have at least two words(can have more than two)值应该至少有两个词(可以有两个以上)
  • only One word Should have number(one or more) in the beginning only 一个词开头应该有数字(一个或多个)
  • Can allow spaces at the end可以在末尾允许空格

Please Help on this as I am new to Regex.请帮助解决这个问题,因为我是 Regex 的新手。

I tried with the below code.我尝试使用以下代码。 It worked for two words but it is not allowing more than two words.它适用于两个词,但不允许超过两个词。 /^[\\d]+[a-zA-Z]*\\s+[a-zA-Z]+$|^[a-zA-Z]+\\s+\\b[\\d]+[a-zA-Z]*\\b$/

Using positive lookahead you can use this regex:使用正向前瞻,您可以使用此正则表达式:

/^^(?=.*\b\d[a-z\d]*\b)(?:\b[a-z\d]+\b[ \t]*){2,}$/i

RegEx Demo正则表达式演示

(?=.*\\b\\d[az\\d]*\\b) is positive lookahead to ensure there is at least one woth that starts with a digit. (?=.*\\b\\d[az\\d]*\\b)是正向前瞻,以确保至少有一个以数字开头的值。

试试这个正则表达式。

/^[\d]+[a-zA-Z]*\s+[a-zA-Z]+$|^([a-zA-Z]+\s+)*(\b[\d]+[a-zA-Z]*\b)+$/

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM