2

I have HTML string in JS where I would like to remove the span element from it.

var HTML= '<div>test</div><p>test</p><span class="removedata">X</span><span>test</span><span class="removedata">X</span>';

After removing Span, it should be look like:

HTML= '<div>test</div><p>test</p><span>test</span>';

I have tried below JavaScript operation but does not work,

HTML = HTML.replace('/<span class="removedata">X</span>/g',"");                                                                                                                                                               

Any inputs?

5
  • Does it have to be with a regular expression? Commented Mar 23, 2017 at 13:52
  • 1
    Removing span from DOM is also an option instead of regex. Commented Mar 23, 2017 at 13:53
  • 1
    Have a look here stackoverflow.com/questions/18464432/… Commented Mar 23, 2017 at 13:55
  • Above span remove string is fixed. So you can share solution based on it. No, I don't need Regular expression. Commented Mar 23, 2017 at 13:55
  • 1
    Possible duplicate of How to replace all occurrences of a string in JavaScript? Commented Mar 23, 2017 at 13:56

6 Answers 6

1

The slash / is a special character in regular expressions and you need to escape it \/:

var html = '<div>test</div><p>test</p><span class="removedata">X</span><span>test</span><span class="removedata">X</span>';
var newHtml = html.replace(/<span class="removedata">X<\/span>/g, '');

console.log(newHtml); // "<div>test</div><p>test</p><span>test</span>"

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

Comments

1

You could try to do it with RegExp.

var str = '<div>test</div><p>test</p><span class="removedata">X</span><span>test</span><span class="removedata">X</span>',
    str2 = str.replace(/<span class="removedata">X<\/span>/g, '');
    console.log(str2);

Comments

0

If you insist on a regular expression, you could

HTML = HTML.replace(/<\/?span class="removedata".[^>]*>/g, '');

1 Comment

This does not worked. I don't need to remove all span but span with class removedata.
0

var HTML= '<div>test</div><p>test</p><span class="removedata">X</span><span>test</span><span class="removedata">X</span>';

var htmlObj = $(HTML);
htmlObj = htmlObj.not(".removedata");

var htmlString = $('<div>').append(htmlObj).html();
console.log(htmlString);
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>

Comments

0

You could replace the text, but a different approach would be also possible:

At some point you are adding the content of HTML to the DOM-tree. When you've done that you could also achieve the "removement" by doing the following:

var elements = document.getElementsByClassName("removedata");
elements[0].parentNode.removeChild(elements[0]);

Comments

0

The below could work.

var html = '<div>test</div><p>test</p><span class="removedata">X</span><span>test</span><span class="removedata">X</span>';
html = html.replace(/<span class="removedata">X<\/span>/g, '');
console.log(html);

You can use the below site if you are new to regex.

https://regex101.com/

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.