var myregexp = /<br\s*\/>((?:(?!<br\s*\/>).)*)$/;
var match = myregexp.exec(subject);
if (match != null) {
result = match[1];
} else {
result = "";
}
說明:
<br\s*/> # match <br /> with optional space
( # capture the following:
(?: # repeat the following, but don't capture (because the outer parens do so already)
(?!<br\s*/>) # assert that it's impossible to match <br />
. # if so, match any character
)* # do this as many times as necessary...
) # (end of capturing group)
$ # ...until the end of the string.
所以我們首先嚐試匹配<br/>
或<br />
等。如果失敗,匹配失敗。如果不是,那麼我們捕獲每個後續字符,直到字符串結束爲止,除非沿途可能匹配另一個<br/>
。這確保我們確實匹配從最後的<br/>
起。結果將反向引用nr。 1(如果最後的<br/>
之後沒有任何內容,則可能爲空)。
我已經用JavaScript過去了一次。 :P – alex 2010-09-14 23:29:57