Ben Amada
Ben Amada

Reputation: 726

Match text via Regex that is within an HTML tag

Via a Regex, I'm trying to match the word one, only when it's within an HTML <p> tag.

  1. <p>zero one two three</p>
  2. zero one two<p>three</p>
  3. <p>zero one <b>two</b></p><p>three</p>
  4. <p>two</p>three one

#1 and #3 above should be matches. It feels like I need a lookahead that makes sure there is a closing </p> tag without an opening <p> tag that comes before it (or a lookbehind that does the opposite). But I can't seem to come up with the right expression. Any ideas are appreciated.

Upvotes: 0

Views: 57

Answers (2)

vks
vks

Reputation: 67968

<p>(?:(?!<\/p>).)*(\bone\b)(?:(?!<\/p>).)*<\/p>

You can try this.Just grab the capture.See demo.

http://regex101.com/r/xT7yD8/12

Upvotes: 2

Avinash Raj
Avinash Raj

Reputation: 174706

You could try the below regex to match the string one which is inside the <p> tag.

\bone\b(?=(?:(?!<\/?p>).)*<\/p>)

DEMO

Upvotes: 1

Related Questions