0

How can I use .replace() with regex for </?span>, as in this question? (this regex would ideally match <span> or </span>, including all things within the span)

I have tried a variety of examples, such as:

.replace(/</?span>/,"")

.replace(/</?span>/g,"")

.replace(/[</?span>]/,"")

.replace(/[</?span>]/g,"")
15
  • The question you linked has a perfectly fitting answer. Commented Dec 22, 2014 at 18:49
  • @Katana314 I agree, assuming that the person knows how to use .match() with regex in the first place Commented Dec 22, 2014 at 18:50
  • Why use regex in the first place? Is the HTML destined for the page? It's easier and safer to remove the nodes than to mess with HTML parsing. Commented Dec 22, 2014 at 18:53
  • 1
    Just a caution in case it applies here - it is not recommended to parse html with regex. stackoverflow.com/questions/1732348/… Commented Dec 22, 2014 at 18:53
  • 2
    @sln: That's sure not going to work. Commented Dec 22, 2014 at 19:02

4 Answers 4

4

In Javascript you need to escape / because JS uses / as regex delimiters and add [^>]* to match anything in span:

.replace(/<\/?span[^>]*>/ig, "")
Sign up to request clarification or add additional context in comments.

1 Comment

need to add g switch too
2

Problem with your code is the regular expression ends at the first /

.replace(/</?span>/,"")
           ^--Thinks this is the closing /

It would need to be escaped.

.replace(/<\/?span>/,"")
           ^ Use \ to escape it

But why use a regular expression to remove elements when it nested elements are going to cause you issues. Use the power of the DOM and do not rely on regular expressions.

function removeSpans(htmlStr) {
    var wrapper = document.createElement("div");
    wrapper.innerHTML = htmlStr;
    var spans = wrapper.getElementsByTagName("span");
    while(spans.length) {
      spans[0].parentNode.removeChild(spans[0]);
   }
   return wrapper.innerHTML;
}


var myHTML = "<span>This is a span</span> Some text <span>This is another span</span>";
var cleanedHTML = removeSpans(myHTML);
document.getElementById("out").innerHTML = cleanedHTML;
<div id="out"></div>

with jQuery:

function removeSpans(htmlStr) {
   var wrapper = $("<div/>").html(htmlStr);
   wrapper.find("span").remove();
   return wrapper.html();
}

1 Comment

Thanks for posting the proper way to clean tags.
0

I see that you included the jQuery tag in your question so I will assume that you can use jQuery. You could use jQuery to solve this issue.

$('span').each(function(){
   $(this).replaceWith($(this).text());
});

This will look for each span element, this element could be any of these:

<span>test</span>
<span class="has-a-class" id="also-an-id">a span with any number of attributes</span>

And replaces it with the text in that span, basically stripping the HTML tag and its attributes:

test
a span with any number of attributes

Comments

0
.replace(/<\/?span.*?>/gi, "")

add ? after * to make it non-greedy match.

11 Comments

That's not going to make a difference.
@squint - Yeah, but shouldn't you tell him why? And, the reason is relative.
@sln: Shouldn't he tell why it would make a difference?
Here yes, because it matches for [^>]*. Personally I keeps non-greedy as a practice, : ]
@elaijuh - Its a tricky tradeoff, if its open-ended, the engine scans for the last > then works backwards, if it's not open-ended based on subsequent sub-expressions, it could still act in a greedy fashion.
|

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.