方法一、遍历字符串并判断

function getNum(str){
    var ary = [];
    var tem = "";
    for(var i=0;i<str.length;i++){
        if(str.charAt(i) >= "0" && str.charAt(i) <= "9"){
            tem += str.charAt(i)
        }else{
            if(tem){
                ary.push(tem);
                tem = "";
            }
        }
    }
    if(tem){
        ary.push(tem);
        tem = "";
    }
    return ary;
}
var str = "faw12aser9907awer788";
console.log(getNum(str));  //["12", "9907", "788"]

方法二、正则匹配

var str = "faw12aser9907awer788";
var reg = /\d+/g;
console.log(str.match(reg));//["12", "9907", "788"]

 

posted on 2017-11-22 22:17  月落无声  阅读(1691)  评论(0编辑  收藏  举报