Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
379 views
in Technique[技术] by (71.8m points)

javascript - RegEx: How can I match all numbers greater than 49?

I'm somewhat new to regular expressions and am writing validation for a quantity field where regular expressions need to be used.

How can I match all numbers greater than or equal to 50?

I tried

[5-9][0-9]+

but that only matches 50-99. Is there a simple way to match all possible numbers greater than 49? (only integers are used)

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

The fact that the first digit has to be in the range 5-9 only applies in case of two digits. So, check for that in the case of 2 digits, and allow any more digits directly:

^([5-9]d|d{3,})$

This regexp has beginning/ending anchors to make sure you're checking all digits, and the string actually represents a number. The | means "or", so either [5-9]d or any number with 3 or more digits. d is simply a shortcut for [0-9].

Edit: To disallow numbers like 001:

^([5-9]d|[1-9]d{2,})$

This forces the first digit to be not a zero in the case of 3 or more digits.


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

1.4m articles

1.4m replys

5 comments

56.9k users

...