0

I'm currently working on a random bible verse generator. When i make a call to the API, it returns text (verse) wrapped in html elements. seems that it was designed that way. How can I get rid of those html elements, to return just the text? this is the output it returns

JCastillo
  • 336
  • 3
  • 13
  • Have you tried using `innerText` property of html element ? Also please post your output as text instead of an image. – stud3nt Mar 05 '20 at 05:08
  • If the response is unreliable, e.g. you have no idea what the text is going to look like. You might consider rendering the HTML, then getting the text out of it. You would just feed the text to a library like https://www.npmjs.com/package/react-html-parser . Or use React's dangerouslySetInnerHTML, if you like to live dangerously. Otherwise, if it is reliable, innerText as stud3nt suggested may be helpful. – Edon Mar 05 '20 at 05:13

1 Answers1

2

Assuming you don't want to render the HTML directly in React, a simple approach would be to use a regex replace:

console.log(data.data.passages[0].content.replace(/<[^>]*>/gm, ""));

Though depending on the potential content of the verses this could give things like excess white space, or fail on esoteric HTML cases.

You could also convert it into an DOM node, then get the text out of it. See this answer.

Jayce444
  • 8,725
  • 3
  • 27
  • 43