2014-07-04 217 views
-1

我有这样的正则表达式JavaScript正则表达式

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

它匹配这个很好55.5 $,但在一些我的测试数据,我有一个像$ 55.5(我的意思是一些值,它有$符号后的空格)。

此链接的答案不适用于我。 Currency/Percent Regular Expression

那么,如何可以改变它接受的空间呢?

+0

更改'([£,$,€]?\ d +'为'([£,$,€]?\ s * \ d +' – hjpotter92

+0

正则表达式对于您的意图看起来太长了... –

+0

这是一个非常难以处理的正则表达式,你想要完全匹配什么? –

回答

1

尝试以下RegEx

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

让我知道它的工作!

Demo Here

+0

谢谢,它的工作 – user3050590

+1

这将匹配',, 123,456789 $%' – Toto

+1

为什么你保留明显的错误原始正则表达式在你的答案? – stema

1

TLDR:

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

科学位

好吧,我猜你没有构建原正则表达式,所以这里有件其中标有:

^ # 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 

这很大一部分是围绕货币金额匹配的东西,所以你可以减少。