Peter Pik Peter Pik - 2 months ago 11
Javascript Question

remove strong tag and its content from string

I'm having below string with this html, i'm wondering how can i remove/filter all strong tags and its text from the string?

html

<p>
<br>
2-3 spsk græsk yoghurt, gerne 10 %
<strong>Dressing: </strong>
<br>
1 lille spsk god mayonnaise
<br>
1-2 tsk æbleeddike
<br>
1-2 tsk honning
<br>
himalayasalt og friskkværnet hvid peber
<br>
</p>


so far i have this, but only seem to remove strong tag if it is at the top

var ingredientsArray = ingredients.split('<br>').map(it => it.trim()).filter(it => !!it && !it.startsWith('<strong>'));

Answer

You can use regex for this:

var ingredientsArray = ingredients.replace(/<strong>[\s\S]*?<\/strong>/g, '')
     .split('<br>').map(it => it.trim()).filter(Boolean);