0

I'm trying to match words with regex outside <a></a> and <img /> tags I have the folowing code that only matches the string outside all tags

/test(?!.*<\/)/g

The test string between the a tags should not match the second two sould

<a>test</a>  test lorem test

any ideas?

cweiske
  • 30,033
  • 14
  • 133
  • 194
waterschaats
  • 994
  • 3
  • 18
  • 32
  • This may not be 100% possible with regex. Solving for this bit is (not between > and ), however you can get into an HTML nesting issue which regex cannot resolve. – akaphenom Jul 05 '16 at 17:12
  • http://stackoverflow.com/questions/6751105/why-its-not-possible-to-use-regex-to-parse-html-xml-a-formal-explanation-in-la – akaphenom Jul 05 '16 at 17:14
  • Don't treat the DOM as a big string you can regexp across. –  May 13 '17 at 08:49

1 Answers1

-1

This should do the work:

var string = "hello<a asdf>hello</a>hello";

var regex = /(.*?)<a.*?>.*?<\/a>(.*?)/g;

console.log(regex.exec(string));
Zimm1
  • 433
  • 5
  • 16