简体   繁体   中英

Regular expression for Number with a hyphen and a decimal

Can I have a regular expression for validate below mentioned type number in javascript and ExtJS.

555-56866.9

I tried this /^[0-9]+(-?[0-9])?(.?[0-9])?/g but it didn't work exactly the way I want.

假设你总是希望匹配三个数字后跟一个破折号后跟五个数字后跟一个句点后跟一个数字,这应该是这样的:

^\d{3}\-\d{5}\.\d$

Let's take your RegEx pattern as a basis:

/^[0-9]+(-?[0-9])?(.?[0-9])?/g

Replace digit character-group with shortcut:

/^\\d+(-?\\d)?(.?\\d)?/g

Fix the pattern so it supports multiple digits in different places:

/^\\d+(-?\\d+)?(.?\\d+)?/g

Correctly escape the dot and make it match start to end:

/^\\d+(-?\\d+)?(\\.?\\d+)?$/g

Done. Check it here: http://regex101.com/r/zS1tC8

^[\d]*-?[\d]*.?[\d]*$

I assumed that you want - and . as optional.

If you want these symbols as mendetory then try

 ^[\d]*-[\d]*.[\d]*$

Hope this will help you..!! :)

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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