Home > database >  How to display a text body with html tags, in React Native?
How to display a text body with html tags, in React Native?

Time:12-15

I have an api that returns some texts from a popular website in my country. You can think of these texts as user's comments. But these comments can includes links ( tag) and also like "< /hr />" tags. Some of them looks like this;

enter image description here

so basically I want an idea about how to manage these kind of texts when we want to display it in React Native?

CodePudding user response:

Maybe this will answer your question:

const regex = /<[^>]*>/mgi
const text = "A text with <strong>html tags</strong>"
const text_without_tags = text.replace(regex, "")
// text_without_tags = 'A text with html tags'

The regular expresion gets every tags in a string (with their attributes). The method replace in this example will replace the tags that the regex got by "" (so nothing).

However, if you want to keep the tags, but only change them into JSX tags, use react-html-parser.

I'm not sure if I understood your goal.

  • Related