使用image url从HTML字符串中删除图像标记

时间:2015-02-05 00:06:47

标签: javascript

http://jsfiddle.net/u1mt0tbz/是我到目前为止的代码。我想获取图像以及标记和任何属性并删除它们。

var image = 'http://www.google.com/logo.gif';
var text = 'alkjsdfkljasd flaksjdflkjasdf klsdjfl skjdflkasjdfl ksjlafj lsdjkflasd jkflasjkdf alsjdf alskdjfal skdfjlaskdjflas kjdflaksdjf laksjdfalskdjf alsdkjflasdjf la <img src="http://www.google.com/logo.gif" /> aljsdfkljaskljdflajsldfkjasljdf asdfjklasdfkjasldkfj asdf';

var newtext = text.replace(image, '');

alert(newtext);

2 个答案:

答案 0 :(得分:0)

尝试使用以下代码删除代码

var newT = text.replace(/<(?:.|\n)*?>/, '')

console.log(newT );

答案 1 :(得分:0)

如何创建元素并将字符串设置为内部HTML,然后查找图像并将其删除?

例如

var container = document.createElement('div');
container.innerHTML = text;

var img = container.querySelector('img[src="' + image + '"]');
container.removeChild(img);

var newtext = container.innerHTML;

JSFiddle