1

What JavaScript regular expression would I use to find out that a string contains an open HTML tag but not a close HTML tag. For example as follows:

var tags = ['h1', 'div', 'span']; // tag to look for

var str1 = 'lorem ipsum <h1>hello world'; // here it is!

var str2 = 'boo foo 123 test'; // this one doesn't have any

var str3 = '<span>boo-boo</span>'; // this has the tags but it is not the case as we only need the ones that have open tags and not close tags.
Adam Fone
  • 11
  • 1
  • Parsing HTML with regex is likely to yield problematic results; regular expressions can't match balanced pairs recursively (though some versions, but not JavaScript, have extensions that make this possible). To do this properly, you'd need a proper parser... and assuming you're talking about client-side JS, you'll probably want to do the parsing on the server (for performance and page weight reasons). – eyelidlessness May 02 '11 at 02:51
  • Is this a simplified example? Will you need to check for nested tags, or flat tags? Can they have attributes? – Kobi May 02 '11 at 04:37

2 Answers2

6

You can't parse HTML with regex. Keeping track of open/close tag pairs requires a stack, and a regular expression is a finite-state machine, which has no stack.

Community
  • 1
  • 1
Wyzard
  • 33,849
  • 3
  • 67
  • 87
-1
'(\<(/?[^\>]+\>)'

This expression is for HTML tags.

M.Azad
  • 3,673
  • 8
  • 47
  • 77