İlker
İlker

Reputation: 2090

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

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?

Upvotes: 2

Views: 3401

Answers (2)

Ryker
Ryker

Reputation: 566

You can use react-native-rich-editor to display the text if you want to keep the html tags

Upvotes: 0

ThomasG2201
ThomasG2201

Reputation: 877

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.

Upvotes: 2

Related Questions