要从</p> <pre> tag

时间:2019-04-14 07:10:31

标签: javascript html regex

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...

1 个答案:

答案 0 :(得分:0)

您可以使用DOMParser()。不需要正则表达式

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)