urfalizeyno urfalizeyno - 4 months ago 7
Javascript Question

How to parse text

I get text field in the document. I get it by

Jquery
.
But sometimes text field includes
<em> </em>
or
<cite> </cite>
tags.
I want the raw text.

I get the text by their id such as;
$('title')[0].innerHTML


It returns me;

further cholesterol transport <em class="a-plus-plus"> Large luteal cells </em> produce <em class="a-plus-plus">more</em>


or

<cite>Sodium bicarbonate</cite> is a white solid that


How can I get the raw text as;

further cholesterol transport Large luteal cells produce more

Sodium bicarbonate is a white solid that


Should I parse the text when I get by their id, or should write a regex that detects
cite
and
em
tags and gets the text inside these tags. What is the best solution for this problem? Is there any solution offered for this type of parsing?

Answer

The jQuery method .text() will get the text content of the selector, which will exclude HTML.

$('title').text();

Similarly in plain JavaScript the .textContent property will also do this by not parsing as HTML.

$('title')[0].textContent;