0

I want to strip the <img> tags and it's contents out of my string that is returned from the database.

Example string

//I want to retrive dummy texts from my string.
var string = "<img src='test.jpg'/>dummy texts <img src='text.jpg'/> dummby text dummy texts";

I have tried

if (string.indexOf('<img') != -1) {
    var newString = $(string).filter(function() {
        return this.tagName != 'img';
    }).text();
}

However, my DB has crappy data and sometimes return

//There is no closure for image tag. 
var string = "<img src='test.jpg'/>dummy texts <img src='text.jpg'/> dummby text dummy texts";

and my code would strip the entire string.

The image tag positions vary.

How do I strip the <img> tags efficiently?

mmmmmm
  • 32,227
  • 27
  • 88
  • 117
FlyingCat
  • 14,036
  • 36
  • 119
  • 198
  • If possible, it would be better to strip these tags from the string in your server side code (php, .net or whatever you are using). – 3dgoo Dec 07 '12 at 01:18

4 Answers4

2
var str  = "<img src='test.jpg'>dummy texts <img src='text.jpg'>",
    text = ​$('<div />').append(str).remove('img').text();

FIDDLE

adeneo
  • 312,895
  • 29
  • 395
  • 388
1

Well, the correct answer is that you don't parse HTML with regex. You need to use a proper XML parser. Fortunately, your browser is one! You could try using jQuery's document fragments:

function stripImages(markup) {
  var fragment = $("<body></body>").html(markup);
  fragment.find("img").remove();
  return fragment.html();
}

var s = "<img src='test.jpg'/>dummy texts <img src='text.jpg'/> dummby text dummy texts"
stripImages(s);

This'll create a document fragment, let you strip the image tags, and then get the sanitized HTML back.

Community
  • 1
  • 1
Chris Heald
  • 61,439
  • 10
  • 123
  • 137
  • Thanks for all the edits you have done. Brian's codes create less codes with regex. +1 though. – FlyingCat Dec 07 '12 at 01:29
  • If this is to secure images from being passed, see my comment on Brian's code. Regex is not a viable solution for securing content from having HTML injected. – Chris Heald Dec 07 '12 at 07:05
1

What worked for me is wrapping the string in a DOM Element, which you could create on the fly like:

var el = $('<div>');
el.html("<img src='test.jpg'>dummy texts <img src='text.jpg'> dummby text dummy texts");
el.find('img').remove();
console.log(el.html());

then simply search and remove specific elements (images or whatever)

gherkins
  • 14,603
  • 6
  • 44
  • 70
  • Thanks for the help. your codes work but I would prefer not wrapping additional div ii my codes +1 though – FlyingCat Dec 07 '12 at 01:28
1

Can also do it without appending first.

var str = "<img src='test.jpg'/>dummy texts <img src='text.jpg'/> dummby text dummy texts";
str = str.replace(/<img[^>]+>/g, '');
Brian Cray
  • 1,277
  • 1
  • 7
  • 19