JavaScript exec()方法

exec() 方法用于检索字符串中的正则表达式的匹配。返回一个数组,其中存放匹配的结果。如果未找到匹配,则返回值为 null。

var str = "我今年25岁明年26岁后年27岁千年24岁";

var reg=/d+/g;

var result;
while(result=reg.exec(str)){
console.log(result[0])
}

//检索出字符串中的所有数字

25
26
27
24

原文地址:https://www.cnblogs.com/h5it/p/10177460.html