USING REGEX
function highlightZeroesWithRegex(str) {
var result = str.split(/(0(?:\n|\t|\s|0|,|\.)*)$/);
return result[0] + `<span class="highlight">${result[1]}</span>`;
}
WITHOUT USING REGEX
- Identify the last sequence of repeating zeros including . and ,
- Enclose the zero sequence in a span element
- Return the modified number
Zero highlighter function
function highlightZeroesWithoutRegex(textContent) {
let num = textContent;
let charList = num.split('');
let isContinuous = true;
let nonZeroNumList = [];
let zeroList = charList.reverse().map(num => {
if(isContinuous && (num === '0' || num === '.' || num === ',')) {
return num;
} else {
isContinuous = false;
nonZeroNumList.push(num);
}
});
let zeroNumString = zeroList.reverse().join('');
let nonZeroNumString = nonZeroNumList.reverse().join('');
const outputString = nonZeroNumString + `<span class="highlight">${zeroNumString}</span>`
return outputString;
}
To iterate through all the div elements
document.querySelectorAll('.num').forEach(element => {
element.innerHTML = highlightZeroesWithRegex(element.textContent)
})
CSS
span.highlight {
font-weight: bolder;
}
Outputs
0.50
10.00
10,000.00
100,050.00
Demo