Regular expression to count number of commas in a string

前端 未结 9 962
故里飘歌
故里飘歌 2020-11-27 05:23

How can I build a regular expression that will match a string of any length containing any characters but which must contain 21 commas?

9条回答
  •  执笔经年
    2020-11-27 06:02

    /^([^,]*,){21}[^,]*$/
    

    That is:

    ^     Start of string
    (     Start of group
    [^,]* Any character except comma, zero or more times
    ,     A comma
    ){21} End and repeat the group 21 times
    [^,]* Any character except comma, zero or more times again
    $     End of string
    

提交回复
热议问题