2016-04-25 76 views
0

我有一个HTML实体更换&看起来像这样:JavaScript的正则表达式mulltiple条件

function htmlEntities(str) { 
return String(str).replace(/&(?!amp;)/g, '&'); 
} 

其工作正常,不是更换&但将取代&

如何添加多个条件正则表达式的功能,以便它不会与其他html实体混淆,如:

' 
" 
> 
< 

我与测试:

var xxx = "1234 &aaa&amp; aaadas 'xxx' \" kkk <aasd> xxxxxxxxxxxxxxxx &lt;"; 

console.log(htmlEntities(xxx)); 

它将取代&lt;成为&amp;lt;,这是不是我想要什么,我需要它离开&lt;非触摸就像例如&aaa&amp;成为&amp;aaa&amp;

希望你明白我的意思,任何想法?

+1

有超过一个正则表达式(向DOM),例如更好的选择http://stackoverflow.com/questions/7394748/whats-the-right-way-to-decode-a-string-that-has-special-html-entities-in-it/http://stackoverflow.com/问题/ 5796718/html-entity-decode –

+0

为什么要重新发明轮子? – NullUserException

回答

1

您可以在正则表达式中使用|来制作替代方案。

+0

'?:',而不是':?'。 –

+0

完美工作..感谢Barmar – Teddybugs