2010-08-11 95 views
1

我有這樣的一段文字中的字符串:刪除回車用正則表達式

<p class="MsoNormal" style="margin-bottom: 0.0001pt; text-align: center; line-height: normal;" align="center"><i style=""><span style="" lang="ES-TRAD">some text 
another text<o:p></o:p></span></i></p> 

<p class="MsoNormal" style="margin-bottom: 0.0001pt; text-align: center; line-height: normal;" align="center"><i style=""><span style="" lang="ES-TRAD">some text more 
and some text more<o:p></o:p></span></i></p> 

如果我做

string.replace(/[\r\n]/g, ""); 

所有回車將被刪除,我只是想刪除那些介於「某些文本」和「另一文本」之間的人,我的意思是在這些跨度內。

在此先感謝。

+5

你知道[你不能用正則表達式解析HTML](http://stackoverflow.com/questions/1732348/regex-match-open-tags-except-xhtml-self-contained-tags/ 1732454#1732454),對吧? – 2010-08-11 04:12:11

+0

但是,當然,您可以使用不規則表達式解析HTML! – 2010-08-11 04:15:34

+0

@Daniel Pryden:不,不過謝謝。 – santiagokci 2010-08-11 04:22:58

回答

1
/[\r\n]+(?=(?:(?!<span\b)[\s\S])*<\/span>)/i 

這將匹配<span>元素內的換行符。它也將匹配<span>標籤以及包含在<span>元素中的任何其他標籤。這可能沒有關係,但我處於一種充分披露的情緒。 ;)

+1

它的工作,非常感謝艾倫! – santiagokci 2010-08-11 14:19:41