Something like this would work:
var output = stringHTML.replace(/<img src='img1.png' \/>/, '');
This will find only the exact substring you've specified (a literal <img src='img1.png' />
) and replace it with an empty string. Note that the forward slash needs to be escaped in the pattern because JavaScript uses forward slashes to delimit regular expression literals.
To remove the first <img>
tag from the input string, regardless of the path or other attributes, you can use this:
var output = stringHTML.replace(/<img.*?\/>/, '');
This will find a literal <img
followed by zero or more of any character, non-greedily, followed by a literal />
. Note that without the global switch (g
) it will only replace the first occurance