如何匹配所有5位数字但跳过第一场比赛

问题描述投票：-2回答：4

var str = "01_12345_02_02_2019_12347_67890_10112_13141";
var empRegex = /(\d{5})/g;
var empNumbers;
empNumbers = str.match(empRegex).toString();
console.log(empNumbers);

javascript arrays regex string
4个回答
1

var str = "01_12345_02_02_2019_12347_67890_10112_13141";
var empRegex = /(\d{5})/g;
var empNumbers = str.match(empRegex);
empNumbers.shift();
empNumbers = empNumbers.toString();
console.log(empNumbers);

3

解：

(empNumbers = str.match(empRegex)).shift();

empNumbers = str.match(empRegex).shift();

代码片段：

var str = "01_12345_02_02_2019_12347_67890_10112_13141";
var empRegex = /(\d{5})/g;
var empNumbers;
(empNumbers = str.match(empRegex)).shift();
console.log(empNumbers);

或者：

var str = "01_12345_02_02_2019_12347_67890_10112_13141", empRegex = /(\d{5})/g;

function matchExceptFirst(str, RE) {
let matches = str.match(RE);
matches.shift();
return matches;
}

var empnumbers = matchExceptFirst(str, empRegex);
console.log(empnumbers);

let excludeFirstMatch = (str, re) => str.match(re).filter((_,i) => (i));

var str = "01_12345_02_02_2019_12347_67890_10112_13141", empRegex = /(\d{5})/g;

let excludeFirstMatch = (str, re) => str.match(re).filter((_,i) => (i));

console.log(
excludeFirstMatch(str, empRegex)
);

var str = "01_12345_02_02_2019_12347_67890_10112_13141", empRegex = /(\d{5})/g;

let excludeFirstMatch = (str, re, len = str.length) => str.match(re).slice(1, len);

console.log(
excludeFirstMatch(str, empRegex)
);

2

How to match on all 5 digit numbers but skip the first match

var str = "01_12345_02_02_2019_12347_67890_10112_13141";
var empRegex = /(?:^.*?\d{5}.*?)?(\d{5})/g;
var empNumbers = [];
var item;

while (item = empRegex.exec( str ))
empNumbers.push(item[1]);

// ---------
console.log(empNumbers);

1

var str = "01_12345_02_02_2019_12347_67890_10112_13141";
var empRegex = /(\d{5})/g;
var empNumbers;
empNumbers = str.match(empRegex).slice(1).toString();
//                              ---------
console.log(empNumbers);

var str = "01_12345_02_02_2019_12347_67890_10112_13141";
var empNumbers = str.split('_').slice(-4).toString();
console.log(empNumbers);

Coding Horror's blog