Remove span with content from string using JS

  html, javascript, string, variables

so I have the following code:

<div class="price price--reduced">
    <span class="price__currency">€</span>
    &nbsp;56,05
    <span class="price__old">
        <span class="price__currency">€</span>
        &nbsp;59,00
    </span>
</div>

I am trying to get the 56,05 and write it into a variable. I can only use JS here. What I wrote so far is:

var element = document.querySelector(".price.price--reduced");
var text = element.innerHTML;
text = text.replace(/</?span[^>]*>/g,"");

The problem now is that the result I get is €&nbsp;56,05€&nbsp;59,00. What I would like to do is not just remove the span from the string but everything it contains as well, only leaving me with the &nbsp;56,05. I couldn’t find the same issue anywhere so maybe you could tell what I miss here.

Source: Ask Javascript Questions

LEAVE A COMMENT