Reputation: 389
Embarrassed to ask, since I should be able to get this, but I've been hitting my head against the wall for a while. I need to replace an img element with a known unique id (here, say, id="abc123"). I would think this should do it, but apparently I'm wrong:
var rgx = '/<img[^>]*id="abc123"[^>]*>/';
var replaced_text = edata.replace(rgx, myreplacementstring);
where edata is a big chunk of html, and myreplacementstring is what I want to replace the img element with. I know in advance that the image element to replace is all lower case, but of course there will be other attributes beyond the id, and they could be on either side of the id. Should be easy right? What am I missing?
Upvotes: 1
Views: 807
Reputation: 20230
Your regular expression works fine.
What you need to do is this:
var rgx = /<img[^>]*id="abc123"[^>]*>/;
instead of:
var rgx = '/<img[^>]*id="abc123"[^>]*>/';
You can see your code working here:
http://jsfiddle.net/Fresh/bMKLU/
Upvotes: 1