Javascript带空格的货币金额的正则表达式

时间:2021-10-01 16:31:25

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).

它很好地匹配55.5美元,但在我的测试数据中,我有一些价值,如55.5美元(我的意思是,它在$符号后面有一个空格)。

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?

那么,如何更改它以接受空格呢?

2 个解决方案

#1


1  

Try following RegEx:

试试以下RegEx:

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

Let me know if it worked!

让我知道它是否有效!

Demo Here

在这里演示

#2


1  

TLDR:

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.

其中大部分都在围绕货币金额进行匹配,因此您可以减少这一点。

#1


1  

Try following RegEx:

试试以下RegEx:

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

Let me know if it worked!

让我知道它是否有效!

Demo Here

在这里演示

#2


1  

TLDR:

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.

其中大部分都在围绕货币金额进行匹配,因此您可以减少这一点。