2014-09-22 74 views

回答

2

你不應該使用HTML的正則表達式來,但我想這樣的事情會工作

var str = '<[email protected]>' + "\n" + 
      'Reply-To: <a href="mailto:[email protected]">noreply </a>' + "\n" + 
      'To:  <a href="mailto:[email protected]">Susno Duandtjuk </a>'; 

var re = /<(?!a|\/a)([^>]+)>/g; 

var out = str.replace(re, "&lt;$1&gt;"); 

console.log(out); 

輸出

&lt;[email protected]&gt; 
Reply-To: <a href="mailto:[email protected]">noreply </a> 
To:  <a href="mailto:[email protected]">Susno Duandtjuk </a> 

Regexp visual

/<(?!a|/a)([^>]+)>/g

+0

雖然這也會取代''。當我在控制檯中運行腳本時,您的輸出不是我所看到的。 – 2014-09-22 08:49:32

+1

對不起,修正了它 – naomik 2014-09-22 08:52:50