Regular expression to count number of commas in a string

前端 未结 9 985
故里飘歌
故里飘歌 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:04

    var valid = ((" " + input + " ").split(",").length == 22);
    

    or...

    var valid = 21 == (function(input){
        var ret = 0;
        for (var i=0; i

    Will perform better than...

    var valid = (/^([^,]*,){21}[^,]*$/).test(input);
    

提交回复
热议问题