简体   繁体   中英

How can I match a bunch of p tags at the end of an html document with javascript regex?

Here is a sample content:

<p> so so so </p>
<div> whatever</div
<p> another paragraph </p>
<div> forever </div>
<p> first of last </p>
<p> second of last </p>

How can I match the last two paragraphs (or any number of consecutive paragraphs) at the end of the above document?

The match output I want is:

<p> first of last </p>
<p> second of last </p>

I tried /(<p>[\s\S]*?<\/p>[\s]*)$/g , but the lazy matching is not working as expected, it sucks all the p tags in between, and matches from the first opening p tag it encounters up to the end of the document.

Note: there might not be paragraphs at the end at all, the regex should not match if there are no paragraphs at the end.

Here we use regex to match all paragraphs and then take the last two elements of the result array.

 let str = `<p> so so so </p> <div> whatever </div <p> another paragraph </p> <div> forever </div> <p> first of last </p> <p> second of last </p>` let reg = /<p>[\w\s]*<\/p>/g; let res = str.match(reg); console.log(res[res.length-2]); console.log(res[res.length-1]);

Adding a negative look ahead to make sure nested paragraphs are not matched seems to do the trick:

/(<p>((??<p>)[\s\S])*?<\/p>[\s]*)+$/g

Would appreciate better suggestions though!

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM