我试图确定一个数字在表格行中重复多少次.当发现3次或更多次时,我需要将td的颜色更改为红色,如下所示:
预期结果:
我的正则表达式正在工作,但我无法获得正确的重复计数并更改td的颜色....
x = $("table").find("tr");
text = "";
x.each(function(line){
number = $(this).text();
check = checkNum(number);
console.log(checkNum(number));
if(check.length > 0){
repeats = check[0][1];
text += "Number "+number[check[0]['index']]+" was repeated "+repeats+ " times on line "+line+"<br>";
$('#results').html(text);
$(this).css('color', 'red');
}
});
function checkNum(num) {
var tempArray = [];
num = num.replace(/\s+/g,"_");
exp = new RegExp(/([0-9])\1{2,}/, "g");
ex = exp.exec(num);
if(ex){
tempArray.push(ex);
}
return tempArray;
}
Run Code Online (Sandbox Code Playgroud)
请检查这个小提琴