0

I am working on a react native project and I have an html string json api response. I am using react-native-render-html to render it, and I can get all paragraphs and apply specific things like number of lines ,etc.. . However I want to get only the first paragraph in the response like text1.

str response='<p class="text">text1</p><p>text2</p><p>text3</p>';

or

str response='<p>text1</p><p>text2</p><p>text3</p>';

If there is empty text like <p></p> at the beginning i want to ignore it and select the next one:

str response='<p></p><p class="text">text1</p><p>text2</p><p>text3</p>';

Is it possible to write a regular expression to get only the content of first paragraph which is text1 ?

Tarif Aljnidi
  • 288
  • 7
  • 16

1 Answers1

0

The following regex should resolve your problem :

const regex = /<p(?:\sclass="[^"]+")?>([^(?:<\/p)]+)<\/p>/i;
const str = '<p>text1</p><p>text2</p><p>text3</p>';

console.log(str.match(regex)[1]);