-1

I have this regular expression

/^[',",\+,<,>,\(,\*,\-,%]?([£,$,€]?\d+([\,,\.]\d+)?[£,$,€]?\s*[\-,\/,\,,\.,\+]?[\/]?\s*)+[',",\+,   <,>,\),\*,\-,%]?$/

It matches this very well $55.5, but in few of my test data I have some values like $ 55.5 (I mean, it has a space after $ sign).

The answers on this link are not working for me. Currency / Percent Regular Expression

So, how can I change it to accept the spaces as well?

Community
  • 1
  • 1
user3050590
  • 1,656
  • 4
  • 21
  • 40

2 Answers2

1

Try following RegEx:

/^[',",\+,<,>,\(,\*,\-,%]?([£,$,€]?\s*\d+([\,,\.]\d+)?[£,$,€]?\s*[\-,\/,\,,\.,\+]?[\/]?\s*)+[',",\+,   <,>,\),\*,\-,%]?$/

Let me know if it worked!

Demo Here

SHEKHAR SHETE
  • 5,964
  • 15
  • 85
  • 143
1

TLDR:

/^[',",\+,<,>,\(,\*,\-,%]?([£,$,€]?\s*\d+([\,,\.]\d+)?[£,$,€]?\s*[\-,\/,\,,\.,\+]?[\/]?\s*)+[',",\+,   <,>,\),\*,\-,%]?$/

The science bit

Ok, I'm guessing that you didn't construct the original regular expression, so here are the pieces of it, with the addition marked:

^ # match from the beginning of the string
[',",\+,<,>,\(,\*,\-,%]?    # optionally one of these symbols
(                           # start a group
   [£,$,€]?                   # optionally one of these symbols
   \s*                        # <--- NEW ADDITION: optionally one or more whitespace
   \d+                        # then one or more decimal digits
   (                          # start group 
      [\,,\.]                   # comma or a dot
      \d+                       # then one or more decimal digits
   )?                         # group optional (comma/dot and digits or neither)
   [£,$,€]?                   # optionally one of these symbols
   \s*                        # optionally whitespace
   [\-,\/,\,,\.,\+]?          # optionally one of these symbols
   [\/]?                      # optionally a /
   \s*                        # optionally whitespace
)+                          # this whole group one or more times
[',",\+,   <,>,\),\*,\-,%]? # optionally one of these symbols
$ # match to the end of the string

Much of this is poking about matching stuff around the currency amount, so you could reduce that.

Phil H
  • 19,928
  • 7
  • 68
  • 105