-2

i have a bug in this code that i cannot seem to solve. if there is only 1 instance of Act, it works as it should. But when there is more than 1 instance of Act, it breaks. Not sure what I am missing here.

//Find all instances of italics
var findItalics = new RegExp(/(<em>.*?<\/em>)/g);
var italicsArray = [];
var italicCount;

while (italicCount = findItalics.exec(searchInput)) {
    italicsArray.push(italicCount[0]);
}

//Find the italics containing the word 'Act'
var keywordItalics = new RegExp(/<em>.*?(Act).*?<\/em>/g);
var keywordItalicArray = [];
var italicCountKeyword;

while (italicCountKeyword = keywordItalics.exec(italicsArray)) {
    keywordItalicArray.push(italicCountKeyword[0]);
}

//Remove all instances of the keyword(s)
for (var tlcs = italicsArray.length - 1; tlcs >= 0; tlcs--) {
    if(italicsArray[tlcs] == keywordItalicArray) {
        italicsArray.splice(tlcs, 1);
    }
}
RW Hammond
  • 113
  • 7

2 Answers2

0

Thanks to @artgb who helped me rethink this.

//Find all instances of italics
var findItalics = new RegExp(/(<em>.*?<\/em>)/g);
var italicsArray = [];
var italicCount;

while (italicCount = findItalics.exec(searchInput)) {
    italicsArray.push(italicCount[0]);
}

//Find the italics containing the word 'Act'
var keywordItalics = new RegExp(/<em>.*?(Act).*?<\/em>/g);
var keywordItalicArray = [];
var italicCountKeyword;

while (italicCountKeyword = keywordItalics.exec(searchInput)) {
    keywordItalicArray.push(italicCountKeyword[0]);
}

//Remove all instances of the keyword(s)
for(var xXx = 0; xXx < keywordItalicArray.length; xXx++){   
    for (var tlcs = italicsArray.length - 1; tlcs >= 0; tlcs--) {
        if(italicsArray[tlcs] == keywordItalicArray[xXx]) {
            italicsArray.splice(tlcs, 1);
        }
    }
}
RW Hammond
  • 113
  • 7
0
var keywordItalics = new RegExp(/<em>.*?(Act).*?<\/em>/g);

Should usually be shortened to:

var keywordItalics = /<em>.*?(Act).*?<\/em>/g;

Where your () are, this would only get a capture of "Act", so to capture whole string in the em, it should be:

var keywordItalics = /<em>(.*?Act.*?)<\/em>/g;

However, a faster way (without regexp) you could get an array of all the emphasized tags just by:

var keywordItalics = document.getElementsByTagName('em');

If you're just trying to get rid of all em's containing "Act", all you should need is:

   document.body.innerHTML = document.body.innerHTML.replace(
     /<em>.*?Act.*?<\/em>/g,
     ''
   );

This should remove all traces of em's containing "Act" in the document (effectively replacing those strings with an empty string, aka nothing). It will cause a reflow, however. If they are inside a containing element besides body, would be better to get containing element first (instead of using body). There are "better" ways of doing this, but this is probably simplest coding-wise.

Update: an easy way to remove em's with "Act" from the array would be:

italicsArray = italicsArray
  .join('_SEP_')  // Convert to string
  .replace(/<em>.*?Act.*?<\/em>/g,'')  // Delete matched entries
  .replace(/(_SEP_)+/g,'_SEP_')  // Collapse multiple seperators 
  .split('_SEP_')  // Convert back to array 
;

This basically uses a seperator _SEP_ (to avoid collisions with strings containing ',') and turns the array into a string, deletes all matches to your regexp, removes what would become undefined entries, and recreates the array in the same name.

jdmayfield
  • 1,400
  • 1
  • 14
  • 26