简体   繁体   中英

Hints for a complex currency regular expression for Javascript?

I need to match an infinite number of figures in a web page.

I need to be able to match all of the following formats:

100 $
99$
$99
$ 8
$.99
$ .8
$ 99.8
.99$
.99 $
9.2 $
1.2$

And the equivalent using commas:

444,333
22,333
1,222
11,111,111
333,333,333,333.01132

Or spaces:

444 333
22 333
1 222
11 111 111
333 333 333 333.01132

This is a really hard one for me. I am used to playing with regexp but I have totally failed to write something bullet proof. Usually http://www.regexlib.com has the solution, but not for this one.

I can't think of another way other than using regexp since it's a plain text search/replace.

Here's a regular expression that will match all the number formats you've provided:

^(?:\$\s*)?(?:(?:\d{0,3}(?:[, ]\d{0,3})*[, ])+\d{3}|\d+)(?:\.\d*)?(?:\s*\$)?$

To break it down:

  • ^(?:\$\s*)? will look for an optional $ at the start of the string, followed by any amount of spaces
  • (?:(?:\d{0,3}(?:[, ]\d{0,3])*[, ])+\d{3}|\d*) will match either a number broken down into groups separated by a comma or space ( \d{0,3}(?:[, ]\d{0,3})*[, ])+\d{3} ) or a string of numbers ( \d+ ) -- so 123,456,789, 123 456 789 and 123456789 would be all matched. The regular expression will not accept numbers with improper grouping (so 123,45,6789 will not be matched)
  • (?:\.\d*)? will match a number with an optional decimal and any number of numbers after
  • (?:\s*\$)?$ will match an optional $ at the end of the string, preceded by any amount of space.

Why write 1 regexp, when you can write several, and apply them in turn?

I'm assuming (?) that you can iterate through line-by-line. Why not try your comma-savvy regexp, followed by your space-savvy regexp etc.? If one matches, then don't bother trying the rest, and store your result and move on to the next line.

what about doing this in 2 steps:

first replace all spaces with ''

then, if number formatting is always the same, you can replace commas with ''

after that, its pretty easy, no?

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