Count the number of integers in a string

后端 未结 4 2205
不思量自难忘°
不思量自难忘° 2020-11-29 09:15

how can I count the number of integers in a string using jQuery or javascript?

For example g66ghy7 = 3

4条回答
  •  一生所求
    2020-11-29 09:30

    I find this to look pretty/simple:

    var count = ('1a2b3c'.match(/\d/g) || []).length
    

    A RegExp will probably perform better (it appears):

    var r = new RegExp('\\d', 'g')
      , count = 0
    
    while(r.exec('1a2b3c')) count++;
    

提交回复
热议问题