2013-10-31 25 views
0

任何人都可以提供我在JavaScript中的正則表達式JavaScript的正則表達式來保持span標籤,並刪除其他

輸入:

<table><tr><td>hello<td/> <span class='xyz'>Hi all </span></tr></table> 

輸出:

hello<span class='xyz'>Hi all</span> 

我可以成功地使用下面的表達式在Java中,但我不知道JavaScript中的等效正則表達式。

String newValue = input.replaceAll("<(?!\\/?span)[^>]+>", ""); 

如果我使用以下中的js表達

input.replace(/ <(/跨度)[^>] +> /克 「」?!?);

我得到輸出

hello Hi all</span> 

,但我想

hello<span class='xyz'>Hi all</span>

+4

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

+0

*「我不知道JavaScript中的等效正則表達式」*您可以使用完全相同的表達。 –

+0

不能抱它已經太晚了。 – naomik

回答

相關問題