2010-09-14 50 views
1

我想查一個字符串中解析出來,如果它沒有<br />然後開始我不想做任何事情
例如mysqtinr = 23435-acs
你可以看到沒有<br />開始 但如果該字符串有下面再來我想解析出來在JavaScript

myString = <br /> some text goes here <b> Some other things </b>: just more test <b> http://website/index.php </b> on line <b> 43 </b> <br /> 1234-acd

我想在最後<br />

浩解析出一切W I能做到這一點 感謝

+1

我已經用JavaScript過去了一次。 :P – alex 2010-09-14 23:29:57

回答

1
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/>之後沒有任何內容,則可能爲空)。

2
var index = myString.lastIndexOf("<br />"); 
if(index > 0) 
    myString = myString.substring(index + "<br />".length); 
+0

這不起作用,因爲標籤'
'和'
'是有效的。見http://jsfiddle.net/y38QH/ – Robert 2010-09-14 23:40:31

+0

你的意思是有效的?他希望最後一個'br'標籤後面的字符串 – 2010-09-14 23:41:38

+0

我的意思是說,如果你試圖解析一個字符串'
',你的方法就不會找到它。 – Robert 2010-09-14 23:42:26