html input patten,Html5 input pattern

"该问题讨论了如何使用正则表达式实现特定的字符串匹配规则。用户需要一个模式来匹配包含至少3个字母字符,后跟一个空格和至少1位数字的字符串,或者反过来,数字在前,字母在后。回答者提供了两种不同的正则表达式解决方案,分别为d+sw{3,}
摘要由CSDN通过智能技术生成

问题

I have this pattern .*?[a-z]([a-z])([a-z])([a-z]).*?(\s+)(\d+) from some online generator but it's not working.

I need pattern which allow:

"minimum 3 char length string, next single space, and next minimum 1 char integer" [abc 1] or the same but in different order "minimum 1 char integer, next single space, and next minimum 3 char length string" [3 gtf].

Thanks.

回答1:

Try this one:

\d+\s\w{3,}|\w{3,}\s\d+

It either matches:

One or more digits (\d+), followed by a single whitespace (\s), followed by three or more word characters (\w{3,}) (this is [a-zA-Z0-9], you can replace this part with [a-zA-Z] if you'd like).

Three or more word characters (this is [a-zA-Z0-9], you can replace this part with [a-zA-Z] if you'd like), followed by a single whitespace, followed by one or more digits.

Regex101

回答2:

The following expression should do what you describe:

[a-z]{3,}\s\d+|\d+\s[a-z]{3,}

Some notes:

The {3,} construct allows you to specify repetitions within a given range. In general, it's {min,max}, but you can leave out either min or (as here) max to have an open-ended range. You can also specify an exact number of repeats by specifying a single number, e.g. {99}.

The expression is essentially two complete expressions switched with alternation. Regular expressions don't allow you to say "exactly these things, in any order".

I've used [a-z] to match the characters in the non-numeric part, as this is what you did in your original example.

来源:https://stackoverflow.com/questions/32970438/html5-input-pattern

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值