get all numbers in a string and push to an array (javascript)

前端 未结 2 476
青春惊慌失措
青春惊慌失措 2021-01-26 17:40

So if I had the following string:

\'(01) Kyle Hall - Osc (04) Cygnus - Artereole (07) Forgemasters - Metalic (10) The Todd Terry Project - Back to the Beat (14)          


        
2条回答
  •  执笔经年
    2021-01-26 18:20

    var str = '(01) Kyle Hall - Osc (04) Cygnus - Artereole (07) Forgemasters - Metalic (10) The Todd Terry Project - Back to the Beat (14) Broken Glass - Style of the Street';
    var nums = str.match(/\d+/g);
    nums.map(function (num) {
        return parseInt(num, 10);
    });
    

    For browsers that does not support Array.prototype.map, use this code:

    var str = '(01) Kyle Hall - Osc (04) Cygnus - Artereole (07) Forgemasters - Metalic (10) The Todd Terry Project - Back to the Beat (14) Broken Glass - Style of the Street';
    var nums = str.match(/\d+/g);
    for (var i = 0; i < str.length; i++) {
        str[i] = parseInt(str[i], 10);
    }
    

提交回复
热议问题