简体   繁体   English

如何从具有特定alt =值的字符串中删除图像元素?

[英]How to remove image elements from string that has specific alt= value?

I have a string that contains HTML image elements that is stored in a var. 我有一个字符串,其中包含存储在var中的HTML图像元素。 I want to remove those image elements that has alt="today" and leave the rest of images untouched . 我想删除那些具有alt =“今天”的图像元素,并保持其余图像不变。

<img src="./images/ok.gif" alt="today" />

I have tried: 我努力了:

var replaced2 = content.replace(/\<img src="./images/ok.gif" alt="today" />\/g,'');

but had no luck. 但没有运气。 Can anyone help me out at all? 任何人都可以帮助我吗? Thanks 谢谢

You don't need a RegExp: 您不需要RegExp:

var replaced2 = content.replace('<img src="./images/ok.gif" alt="today" />','');

jsFiddle 的jsfiddle

Try this as your regular expression: 试试这个作为你的正则表达式:

/<img src=".\/images\/ok.gif" alt="today" \/>/g

Gotta make sure to escape any forward slashes because they denote a regexp 必须确保逃避任何正斜杠,因为它们表示正则表达式

/<regexp syntax>/

您需要关注的是:JavaScript RegExp

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM