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?

Ankit Shah
  • 137
  • 1
  • 13
  • Does it have to be with a regular expression? – Vergil Penkov Mar 23 '17 at 13:52
  • 1
    Removing span from DOM is also an option instead of regex. – dfsq Mar 23 '17 at 13:53
  • 1
    Have a look here http://stackoverflow.com/questions/18464432/how-to-remove-span-tag-from-the-string – geo Mar 23 '17 at 13:55
  • Above span remove string is fixed. So you can share solution based on it. No, I don't need Regular expression. – Ankit Shah Mar 23 '17 at 13:55
  • 1
    Possible duplicate of [How to replace all occurrences of a string in JavaScript?](http://stackoverflow.com/questions/1144783/how-to-replace-all-occurrences-of-a-string-in-javascript) – Pugazh Mar 23 '17 at 13:56

6 Answers6

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);
kind user
  • 40,029
  • 7
  • 67
  • 77
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>"
TimoStaudinger
  • 41,396
  • 16
  • 88
  • 94
0

If you insist on a regular expression, you could

HTML = HTML.replace(/<\/?span class="removedata".[^>]*>/g, '');
Vergil Penkov
  • 380
  • 2
  • 11
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>
Jatin patil
  • 4,252
  • 1
  • 18
  • 27
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]);
edi
  • 917
  • 7
  • 17
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/