0

I think I need a regular expression (javascript) that can only find a string if it is NOT inside an element. IE if I wanted to find the string "Hello" but not in the div "popup"

<div class="popup">optional content Hello world and more</div>
--No Match--

<div>This is more content Hello and welcome</div>
--Match--

Can someone point me in right direction, negative lookahead?

ok should mention how im getting it :) The find already has a reg ex to "hopefully" ignore searching inside html tags but this needs to work on top of it.

var find = "((?![^<]*>) " + data[i].GlossaryWord.trim() + " )";
        
var replace = " <a class=\"gobig tooltip\" " + "title=\"" + data[i].GlossaryDescription + "\">" + data[i].GlossaryWord.trim().toLowerCase() + "</a> ";
var elementContent = elementContent.replace(new RegExp(find, 'gi'), replace);
CerIs
  • 527
  • 3
  • 6
  • 14

1 Answers1

0

Maybe easier to use CSS selectors. Something like:

document.body.querySelectorAll('div:not(.classy)')
Jamieson Rhyne
  • 428
  • 3
  • 10
  • wish I could use jquery selector but the content isnt in a webpage, its just html in a javascript string – CerIs Jul 15 '15 at 21:03
  • @CerIs I think you can do that, check this out: http://stackoverflow.com/questions/3754092/how-to-get-an-html-element-from-a-string-with-jquery – Jamieson Rhyne Jul 15 '15 at 22:27
  • yip think that might work, will give it a go - thanks :) – CerIs Jul 16 '15 at 23:41
  • actually dont think I can use jquery. The selector will let me copy only the html I want to search into a seperate var but when I find/replace inside this new var it wont have all the data. IE i dont want to search inside this div but I still need it to exist at the end, make sense? I think regex is only way to go? – CerIs Jul 17 '15 at 09:51
  • @CerIs I'm not sure I understand, but you could create an element that contains multiple divs. Just concatenate a div tag around the whole string and pop it into a jquery element. – Jamieson Rhyne Jul 17 '15 at 11:37