最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

javascript - Regex to allow negative values - Stack Overflow

programmeradmin1浏览0评论

I am hoping to find someone here which can give me a hand with some regex I need to modify.

This regex is being used in JavaScript for some validation:

/^([A-Z]{3} [0-9]+\.[0-9]{1,2}(, )?)+$/

This will correctly validate the following values:

EUR 20.3, USD 9.0

GBP 8.8

I would like to modify the regex to also accept negative values such as:

EUR -20.3, USD -9.0

I thank you for your help :)

Regards Gabriel

I am hoping to find someone here which can give me a hand with some regex I need to modify.

This regex is being used in JavaScript for some validation:

/^([A-Z]{3} [0-9]+\.[0-9]{1,2}(, )?)+$/

This will correctly validate the following values:

EUR 20.3, USD 9.0

GBP 8.8

I would like to modify the regex to also accept negative values such as:

EUR -20.3, USD -9.0

I thank you for your help :)

Regards Gabriel

Share Improve this question edited Apr 7, 2011 at 9:49 Noufal Ibrahim 72.9k13 gold badges140 silver badges174 bronze badges asked Apr 7, 2011 at 9:46 Gabriel SpiteriGabriel Spiteri 4,97812 gold badges45 silver badges60 bronze badges
Add a ment  | 

3 Answers 3

Reset to default 5

surely just add in:

-?

which should optionally match '-'

/^([A-Z]{3} -?[0-9]+\.[0-9]{1,2}(, )?)+$/

In the exact same way you check for the a , but xith negative signe -

Try something like this ([A-Z]{3} -?[0-9]+.[0-9]{1,2},?)+

try this:

/^([A-Z]{3} -?[0-9]+(\.[0-9]{1,2})?,?)+$/

the decimal places are now optional

GBP -1, GBP -1.1 should be valid

发布评论

评论列表(0)

  1. 暂无评论