1

I have a HTML code like this:

<span>This is HTML.</span>&nbsp; This is &nbsp;&nbsp; paragraph.

And I want to replace the span tag along with &nbsp;.

My output would be like this:

This is HTML.This is &nbsp;&nbsp; paragraph.

This is what I've tried:

myvar = "<span>This is HTML.</span>&nbsp; This is &nbsp;&nbsp; paragraph.";
newContent = myvar.replace(/<\/?span[^>]*>/g,"");
murli2308
  • 2,976
  • 4
  • 26
  • 47
Manoj
  • 11
  • 1

1 Answers1

0

Just use replace several times. Since, unless you use a regex, it only replaces the first occurrence of something, you won't lose the extra &nbsp;.


let initialString = '<span>This is HTML.</span>&nbsp; This is &nbsp;&nbsp; paragraph.';
let finalString = initialString.replace('<span>', '').replace('</span>', '').replace('&nbsp;', '');

Matt Ellen
  • 11,268
  • 4
  • 68
  • 90