Adam Fone
Adam Fone

Reputation: 11

What JavaScript regular expression would I use to find out that a string contains an open HTML tag but not a close HTML tag?

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.

Upvotes: 1

Views: 288

Answers (2)

M.Azad
M.Azad

Reputation: 3763

'(\<(/?[^\>]+\>)'

This expression is for HTML tags.

Upvotes: -1

Wyzard
Wyzard

Reputation: 34563

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.

Upvotes: 6

Related Questions