25

So I have following string:

var s = '<span>Some Text</span> Some other Text';

The result should be a string with the content "Some other Text".

I tried...

var $s = $(s).not('span');

...and a lot of other stuff with remove(), not(), etc. but nothing worked.

Any suggestions? I could match the string with regex, but I prefer a common jQuery solution.

edit:

I do not search a solution with regex, I'm just wondering why this example does not work: http://jsfiddle.net/q9crX/150/

Abdul Munim
  • 18,869
  • 8
  • 52
  • 61
Fidelis
  • 339
  • 1
  • 4
  • 11

6 Answers6

57

You can wrap your string in a jQuery object and do some sort of a manipulation like this:

var removeElements = function(text, selector) {
    var wrapped = $("<div>" + text + "</div>");
    wrapped.find(selector).remove();
    return wrapped.html();
}

USAGE

var removedSpanString = removeElements("<span>Some Text</span> Some other Text", "span");

The beauty of this approach is that you can specify a jquery selector which to remove. i.e. you may wish to remove the <th> in a string. This would be very handy in that case. Hope this helps!

Abdul Munim
  • 18,869
  • 8
  • 52
  • 61
  • Ok thank you, this one worked ans isn't a regex! :) But check out the edit of my question, maybe there is a solution for my jsfiddle-example as well. – Fidelis Aug 24 '12 at 13:22
15

A very simple approach:

var html = '<span>Remove <b>tags &amp; entities</b></span>';
var noTagText = $(html).text();
// ==> noTagText = 'Remove tags & entities'

Note that it will remove tags but also html entities.

yolenoyer
  • 8,797
  • 2
  • 27
  • 61
11

This may suit your needs:

<([^ >]+)[^>]*>.*?</\1>|<[^/]+/>

Regular expression visualization

Debuggex Demo

In JavaScript:

$s = s.replace(/<([^ >]+)[^>]*>.*?<\/\1>|<[^\/]+\/>/ig, "");

It also removes self-closing tags (e.g. <br />).

sp00m
  • 47,968
  • 31
  • 142
  • 252
4

Just remove html tag like this

DEMO

var s = '<span>Some Text</span> Some other Text';
var r = /<(\w+)[^>]*>.*<\/\1>/gi;
s.replace(r,"");

Answer given over here :http://www.webdeveloper.com/forum/showthread.php?t=252483

Pranay Rana
  • 175,020
  • 35
  • 237
  • 263
  • @sp00m - thanks for into but wokrs with given input..its need to modify as op want – Pranay Rana Aug 24 '12 at 13:14
  • Yes this regex is really great :) It should work for me, but I still have this problem here: http://jsfiddle.net/q9crX/150/ – Fidelis Aug 24 '12 at 13:28
  • I've downvoted this answer because I don't believe that using regex to parse HTML is valid. Especially when this question is using jQuery which can easily manipulate the DOM nodes. – David Yell Jan 13 '14 at 12:24
  • This provides a better solution. http://stackoverflow.com/questions/10585029/parse-a-html-string-with-js/10585079#10585079 – David Yell Jan 13 '14 at 14:18
3

Just do the following:

<script type="text/javascript" src="http://ajax.googleapis.com/ajax/libs/jquery/1.4/jquery.min.js"></script>

<script>
$(document).ready(function(){
 $("span").not("span div").each(
 function(index, element) {
$("span").remove();
 }
 );
 });

</script>

<span>Some Text</span> Some other Text
Conrad Lotz
  • 8,200
  • 3
  • 23
  • 27
2

Check link

e.g. More Specific to your case :-

var s = '<span>Some Text</span> Some other Text';
var $s = s.replace(/<span>(.*)<\/span>/g, "");
Jayendra
  • 52,349
  • 4
  • 80
  • 90