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,"");
3
  • Please show us what you have tried yourself. Commented Oct 15, 2019 at 8:43
  • myvar = "<span>This is HTML.</span>&nbsp; This is &nbsp;&nbsp; paragraph."; myvar.replace(/<\/?span[^>]*>/g,""); Commented Oct 15, 2019 at 8:45
  • Please check this link Commented Oct 15, 2019 at 8:46

1 Answer 1

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;', '');

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.