2014-12-28 16 views
-4

我試圖從MS Word的VBA宏(我目前使用的PC沒有太多的東西),它並不是很好,所以我試着用它Firefox控制檯。我想做一個錨標記去除功能

這是我當前的代碼:

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>')); 

現在,我想輸出是

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

但是,相反的是,我只得到</div>

我不知道什麼地方出了錯。

+0

不要用正則表達式解析HTLM。 http://stackoverflow.com/questions/1732348/regex-match-open-tags-except-xhtml-self-contained-tags –

+0

如果你給我們一些見解,你正在嘗試做什麼,也許我們可以幫助你以更好的方式來做到這一點。這應該與jQuery相當簡單,但你使用jQuery? –

回答

0

試試這個:

  1. 你需要使用 「G」,以取代所有實例。
  2. 你正在做的是刪除標籤之間的所有內容。

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

=====編輯=====

oneliner:

function stripAnchorTags(html) { 
    return html.replace(/\<a.*?\>(.*?)\<\/a\>/g, '\$1'); 
} 
+0

它的工作原理!雖然不知道「g」。 – BBBig

+0

很高興它:)你能接受答案嗎? – redmoon7777