2013-04-10 3 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

+0

Это то, что вы ошибаетесь или что-то в этом роде? –

+1

Вы задали 16 вопросов. Пришло время приложить немного усилий для правильного форматирования вашего сообщения. – 2013-04-10 14:24:08

ответ

0

Это уже трудно «подсчета» внутри регулярного выражения, но то, что вы спрашиваете, не может быть сделано с одним. Регулярное выражение не имеет памяти о том, что оно уловлено раньше, поэтому в случае второго (2) у вас не будет инструмента, чтобы знать, что он уже был сопоставлен.

Теперь вот интересный инструмент функции замены. Вы можете указать функцию обратного вызова, а здесь вы можете немного проверить. Аргументы обратного вызова будет:
1) Вся соответствует строка
2) захватывая группы (так от 0 до п аргументов)
3) Положение матча
4) начальная строка

Так в основном , вы можете, например, захватить номер, который видите, и если вы его уже видели, ничего не делать (вернуть ту же строку, т. е. аргументы [0]), и другие вещи, чтобы помочь вам ...