2013-04-10 51 views
0

我有一個這樣的字符串來自數據庫。如何使用正則表達式和javascript控制數據

str="This is first string.Exception No. 1: Unprotected vertical openings in accordance with 8.2.5.8 shall be permitted.Exception No. 2:Exception No. 1 to 8.2.5.6(1) shall not apply to patient sleeping and treatment rooms.This is test ". 

我用這condtion打破了新的生產線

str = str.replace(/(\s\(\d+\)|exception\s*\:*)/gi, "<br /><br />$1&nbsp"); 

,但我得到的輸出是這樣的: 這是第一個字符串

Exception No. 1: Unprotected vertical openings in accordance with 8.2.5.8 shall be permitted. 

Exception No. 2: 

Exception No. 1 to 8.2.5.6(1) shall not apply to patient sleeping and treatment rooms. 

,但我想得到這樣的輸出這個: -

Exception No. 1: Unprotected vertical openings in accordance with 8.2.5.8 shall be permitted. 

Exception No. 2:Exception No. 1 to 8.2.5.6(1) shall not apply to patient sleeping and treatment rooms. 

這是測試。

我怎樣才能this..thanks提前...

回答

2

我建議這一個:

var str = str.replace(/(Exception\s+No\.\s+\d+\:)/g, "<br /><br />$1&nbsp;"); 

捕獲整個開始表達與:避免醒目的引文。請注意,我刪除了i標誌,它似乎沒有用處。

您還錯過了&nbsp;末尾的;

0
var str="Exception No. 1: Unprotected vertical openings in accordance with"+ 
     " 8.2.5.8 shall be permitted.Exception No. 2:Exception No. 1 to 8.2.5.6(1)"+ 
     " shall not apply to patient sleeping and treatment rooms.This is test ", 
    expected = "<br /><br />Exception No. 1: Unprotected vertical"+ 
       " openings in accordance with 8.2.5.8 shall be permitted."+ 
       "<br /><br />Exception No. 2:Exception No. 1 to 8.2.5.6(1)"+ 
       " shall not apply to patient sleeping and treatment rooms."+ 
       "This is test "; 
str.replace(/(Exception\s+No\.\s*\d\:)/gi,"<br /><br />$1") === expected 
+0

謝謝你的回答,但我再次編輯了這個問題。假設情況如此,假設前面有一些句子? – 2013-04-10 09:03:17

+0

'這是第一個字符串.'將在第一行,'Exception No. 1 ...'將在下一個 – user2264587 2013-04-10 09:11:47

相關問題