2014-07-04 214 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 

這很大一部分是圍繞貨幣金額匹配的東西,所以你可以減少。