Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

react-native – get text inside all <b> tags in a html string

Let’s say i have a html string in react native. Since theres no document in react-native, im a bit confused on how this can be achieved

const htmlString = '<div style="margin-left: 10px">I have three dogs. One is called <b>Tiger</b>, the second is called <b>Lion</b>, and the third is called <b>Panda</b>.</div>'

I want to extract the text inside each <b> tag. There can be more than 10 <b> tags in this html string. The <b> tag can also be nested on many levels within the string

Expected output:

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

Tiger
Lion
Panda

This data can be stored in array or as object.
Please note: This is in react-native

Thank you

I tried something like

htmlString.split('<b>').pop().split('</b>')[0]

that returns only Panda. cannot find a way to get the previous ones too

>Solution :

You can try this:

const reg = /<b>(.*?)<\/b>/g
for (const match of htmlString.matchAll(reg)) {
  console.log(`matched: ${match[1]}`);
}

Result:

matched: Tiger
matched: Lion
matched: Panda
Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading