I need some expert advice.
The project I'm working on is report generated in HTML.
I need to use javascript to
1. Find a number and extract a part of it
2. Increment/Decrement the extracted part
3. Replace the number with new value
4. Replace other elements
Also, the html I'm working with has no tags or ids, so I'm forced to grab the entire content.
Here is what I have so far: http://jsfiddle.net/y6Hy7/3/
var els = document.getElementsByTagName("*");
for(var i = 0, l = els.length; i < l; i++) {
var el = els[i];
var txt = els[i].textContent; //grabbing the text
var patt = /\d+(?=,[0-9]{3}\sPARTS)/g; //regex pattern only grab the first 2 digits
var res = patt.exec(txt); //execute regex on text
if (res !== null) { //see if its not null
var toNum = parseInt(res,10) +10;
} //convert to int and add some fixed value
el.innerHTML = el.innerHTML.replace(patt, toNum); //replace the original
el.innerHTML = el.innerHTML.replace("text need to be changed and looped", "replaced");
}
It works, but keep increasing the replaced value too many times. How can I fix that?