2013-04-10 44 views
-1

格式化JavaScript中的字符串,這是我的字符串我從數據庫如何使用正則表達式

var str=" The requirements of this chapter apply to the following:(1) New 
buildings or portions thereof used as health care occupancies (see 1.4.1)(2) 
Additions made to, or usedas, a health care occupancy (see 4.6.6 and 18.1.1.4)(2) 
and test.Exception: Exception no 1 The requirement of 18.1.1.1.1 shall not apply 
to additions classified as occupancies other than health care that are separated 
from the health care occupancy in accordance with 18.1.2.1(2) and conform to the 
requirements for the specific occupancy in accordance with Chapters 12 through 17 
and Chapters 20 through 42, as appropriate.(3) Alterations, modernizations, or 
renovations of existing health care occupancies (see 4.6.7 and 18.1.1.4)(1)(4) 
Existing buildings or portions thereof upon change of occupancy to a health care 
occupancy (see 4.6.11)Exception *: Facilities where the authority having 
jurisdiction has determined equivalent safety has been provided in accordance 
with Section 1.5." 

我已經使用了以下條件得到

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

從中我得到:

The requirements of this chapter apply to the following: 

(1) New buildings or portions thereof used as health care occupancies (see 1.4.1) 

(2) Additions made to, or usedas, a health care occupancy (see 4.6.6 and 18.1.1.4) 

(2) and test. 

Exception: 

Exception no 1 The requirement of 18.1.1.1.1 shall not apply to additions classified as occupancies other than health care that are separated from the health care occupancy in accordance with 18.1.2.1(2) and conform to the requirements for the specific occupancy in accordance with Chapters 12 through 17 and Chapters 20 through 42, as appropriate. 

(3) Alterations, modernizations, or renovations of existing health care occupancies (see 4.6.7 and 18.1.1.4) 

(1) 

(4) Existing buildings or portions thereof upon change of occupancy to a health care occupancy (see 4.6.11) 

Exception *: Facilities where the authority having jurisdiction has determined equivalent safety has been provided in accordance with Section 1.5. 

但我期望的輸出是

The requirements of this chapter apply to the following: 

(1) New buildings or portions thereof used as health care occupancies (see 1.4.1) 

(2) Additions made to, or usedas, a health care occupancy (see 4.6.6 and 18.1.1.4)(2) and test. 

Exception: Exception no 1 The requirement of 18.1.1.1.1 shall not apply to additions classified as occupancies other than health care that are separated from the health care occupancy in accordance with 18.1.2.1(2) and conform to the requirements for the specific occupancy in accordance with Chapters 12 through 17 and Chapters 20 through 42, as appropriate. 

(3) Alterations, modernizations, or renovations of existing health care occupancies (see 4.6.7 and 18.1.1.4)(1) 

(4) Existing buildings or portions thereof upon change of occupancy to a health care occupancy (see 4.6.11) 

Exception *: Facilities where the authority having jurisdiction has determined equivalent safety has been provided in accordance with Section 1.5. 

在此先感謝..

+0

我認爲這將是如果你能夠自己處理這個問題之後,能夠證明你正在努力解決這個問題的哪一部分,那更好。 – Peter 2013-04-10 14:23:05

+0

是你得到錯誤或什麼? – 2013-04-10 14:23:34

+1

您提到了16個問題。現在是時候多加一點努力來正確格式化您的文章。 – 2013-04-10 14:24:08

回答

0

它已經很難正則表達式裏面的「計數」,但你問不能用一個做什麼。正則表達式沒有任何回憶,因此在第二個(2)的情況下,你將不會有任何工具知道它已經被匹配。

現在,來了一個有趣的替換函數的工具。你可以指定一個回調函數,在這裏你可以做一些檢查。回調參數將是:
1)整個匹配的字符串
2)捕獲組(所以0到n的參數)
3)匹配
4)初始字符串

所以基本上的位置,你可以捕獲你看到的數字,如果你已經看到它們,什麼都不做(返回相同的字符串,即參數[0]),以及其他的東西來幫助你...