2015-08-13 54 views
-3

在Javascript中,我需要正則表達式來允許在文本框中逗號分隔的任何字符。Javascript正則表達式允許逗號分隔的任何字符

例子:
有效的字符串爲:

ie, ch, mz 
en, fa, ta 
/path/xyz , /test/apps/    
msg_abc_chec.ts, ss_msg_abc_chec.ts  
com.app.fr, vi.dsx;gui  
909.33.33.12312:343234, 33.23.33  
800x480, 200x480, 1200x1060 , 400x160  
Main app, sub app  
SomeAudOut#1 , SomeVidIn#3 

無效的字符串是:

ie,, ch, mz - 2 commas without string 
en, fa, ta, - comma at the end 
SomeAudOut#2 - no comma separated value   
+0

查看[Demo](https://regex101.com/r/jD1lQ3/1) – Tushar

回答

1

您可以使用

/^(?:[^,\n]+,)+[^,\n]+$/gm 

參見demo

  • ^ - 線的開始
  • (?:[^,\n]+,)+ - 的
    • [^,\n]+, 1或多個序列 - 比,(和換行其它1個或多個字符,但它僅僅是爲多線模式)和一個,
  • [^,\n]+ - 除,\n以外的1個或多個字符(在多線模式下測試時,還需要\n)。
  • $ - 線

的端部。在情況下,你也希望排除ie, ch, , , mz樣線,use this regex

^(?!.*,\s*,)(?:[^,\n]+,)+[^,\n]+$ 

這裏,(?!.*,\s*,)防止符合具有連續2個逗號用空格隔開。

+0

此輸入通過:'/ test/apps /,,/ test/apps /' –

+0

不,它[不] (https://regex101.com/r/vR2lQ8/4)與我最後的建議。 –

0

應該符合這樣的:

((.*)+,)* 
相關問題