这是我从数据库中得到的字符串
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 ");
我从中得到:
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 :(得分:0)
在正则表达式中已经很难“计算”,但你所要求的不能用一个。正则表达式对之前所捕获的内容没有任何记忆,所以在第二个(2)
的情况下,你将没有任何工具可以知道它已被匹配。
现在,这里有一个有趣的替换功能工具。你可以在这里指定一个回调函数,你可以做一些检查。回调参数将是:
1)整个匹配的字符串
2)捕获组(所以0到n个参数)
3)比赛的位置
4)初始字符串
所以基本上,你可以例如捕获你看到的数字,如果你已经看过它们,什么也不做(返回相同的字符串,即参数[0]),以及其他可以帮助你的东西...