-4

I tried VBA macro from MS Word(the PC i'm currently using doesn't have much stuff), it did not turn out to be great, so I tried it with Firefox console.

This is my current code:

function stripAnchorTags(html) {
    return html.replace(/\<a.*\>.*\<\/a\>/, '');
}
console.log(stripAnchorTags('<a href="image location" style="margin-left: 1em; margin-right: 1em;"><img src="image location" border="0" height="400" width="640"></a></div><br><a href="image location" style="margin-left: 1em; margin-right: 1em;"><img src="image location" border="0" height="400" width="640"></a></div>'));

Now, the output I want was

<img src="image location" border="0" height="400" width="640"></div><br><img src="image location" border="0" height="400" width="640"></div>

But instead of it, I only get </div>

I have no idea what went wrong.

j08691
  • 204,283
  • 31
  • 260
  • 272
BBBig
  • 3
  • 1
  • Don't parse HTLM with regex. http://stackoverflow.com/questions/1732348/regex-match-open-tags-except-xhtml-self-contained-tags – AJ Richardson Dec 28 '14 at 19:26
  • If you give us some insight as to what you are trying to do, maybe we can help you with a better way of doing it. This should be pretty simple to do with jQuery, but are you using jQuery? – AJ Richardson Dec 28 '14 at 19:52

1 Answers1

0

try this:

  1. you need to use "g" to replace all instances.
  2. what you were doing was deleting everything between the tag.

    function stripAnchorTags(html) { html = html.replace(/\</a>/g, ''); html = html.replace(/\]*>/g, ''); return html; }

===== EDIT =====

oneliner:

function stripAnchorTags(html) {
    return html.replace(/\<a.*?\>(.*?)\<\/a\>/g, '\$1');
}
redmoon7777
  • 4,498
  • 1
  • 24
  • 26