-5

I am trying to select all the text between

tags from a parent tag. How to do this. using regex

This is my sample string

<p>sddsd</p>
<pre>
<p>line 1</p>
<p>line 2</p>
<p><line 3</p>
</pre>

I want to select only this

line 1
line 2
line 3

I am using this regex, but it is selecting only first line of p tag

<pre>\n<p>(.+)<\/p>

use this website to run your regex

https://regex101.com/

Please help me...

Nazish Fraz
  • 94
  • 1
  • 9

1 Answers1

0

You could do with DOMParser() .No need regex

var str = '<p>sddsd</p><pre><p>line 1</p><p>line 2</p><p>line 3</p></pre>';

var parser = new DOMParser();
var htmlDoc = parser.parseFromString(str, 'text/html');
console.log(htmlDoc.querySelector('pre').textContent)
prasanth
  • 22,145
  • 4
  • 29
  • 53
  • The OP HTML is invalid, so the results will be implementation dependent. Most likely the P elements will be moved out of the PRE element, so the result **might** be an empty string. – RobG Apr 14 '19 at 07:37
  • i think its a typo on third element of `

    `

    – prasanth Apr 14 '19 at 07:41