Javascript 如何使用正则表达式和str.replace格式化

Javascript 如何使用正则表达式和str.replace格式化,javascript,html,regex,Javascript,Html,Regex,我正在使用这个正则表达式 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 used as, a health care occupancy (see 4.6.6 and 18.1.1.4) Excep

我正在使用这个正则表达式

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 used as, a health care occupancy (see 4.6.6 and 18.1.1.4) Exception: 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) (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(/(\(\d+\)|exception\s*\:*)/gi, "<br /><br />$1&nbsp");
var str=“本章的要求适用于以下情况:(1)用作医疗保健用房的新建筑或其部分(见1.4.1)(2)增加或用作医疗保健用房(见4.6.6和18.1.1.4)例外情况:18.1.1.1.1的要求不适用于根据18.1.2.1(2)与医疗保健占用区分开并符合第12章至第17章和第20章至第42章(视情况而定)规定的特定占用区要求的非医疗保健占用区现有医疗保健用房的改建、现代化或翻新(见4.6.7和18.1.1.4)(4)现有建筑物或其部分在占用改为医疗保健用房后(见4.6.11)例外情况*:具有管辖权的当局已确定已根据第1.5节提供同等安全性的设施。”;
str=str.replace(/(\(\d+\)| exception\s*\:*)/gi,“

$1”);
我在哪里得到这样的输出

18.1.1.1.1本章的要求适用于以下各项:

(1) 用作医疗保健用房的新建筑物或其部分 (见1.4.1)

(2) 增加或用作医疗保健占用(见4.6.6 和18.1.1.4)

例外情况:18.1.1.1.1的要求不适用于添加物 分类为除医疗保健以外的分离职业 根据第18.1.2.1条的规定,从医疗服务占用中扣除

(2) 并符合本项目的具体占用要求 根据第12章至第17章和第20章至第42章,如下所示: 合适

(3) 对现有健康状况的改变、现代化或翻新 护理占用率(见4.6.7和18.1.1.4)

(4) 现有建筑物或其部分在占用权变更为 a医疗占用(见4.6.11)

但我想要的结果是

18.1.1.1.1本章的要求适用于以下各项:

(1) 用作医疗保健用房的新建筑物或其部分 (见1.4.1)

(2) 增加或用作医疗保健占用(见4.6.6 和18.1.1.4)(2),并符合具体要求 根据第12章至第17章和第20章的规定占用 至42,视情况而定

例外情况:18.1.1.1.1的要求不适用于添加物 分类为除医疗保健以外的分离职业 根据第18.1.2.1条的规定,从医疗服务占用中扣除

(3) 对现有健康状况的改变、现代化或翻新 护理占用率(见4.6.7和18.1.1.4)

(4) 现有建筑物或其部分在占用权变更为 a医疗占用(见4.6.11)


在这里,它再次打断了该值的
(2)
(请参见4.6.6和18.1.1.4)(2)。如何获得此格式?

在正则表达式中,尝试仅在前面有空格的
\(\d)\
上拆分:

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

$1”;
在括号中的数字之前查找非结束括号的内容。你必须在例外部分假装出来。我决定在单词“exception”之前找一个非字母的。这确保了它是一个完整的词,至少在前面。(不过,公平地说,我想不出一个以“expression”结尾的英语单词。)

请注意,当匹配器查找匹配的内容时,JavaScript是如何按照正则表达式中参数的顺序而不是参数的顺序对匹配的字符串进行编号的。因此不匹配的部分(因为or)仍然计入1美元、2美元、3美元和4美元

str = str.replace(/([^)])(\(\d+\))|([^a-zA-Z])(exception\s*\:*)/gi, "$1$3<br /><br />$2$4&nbsp");
str=str.replace(/([^)])(\(\d+\))([^a-zA-Z])(例外情况*:*)/gi,“$1$3

$2$4”;
您可以在此处测试Javascript正则表达式:


(这就是我所做的,让这个表达式工作。)

你可以将
(?:\(\d+\)*
合并到你的正则表达式中,除非我错过了你想要的?

使用

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

$1$2”);

它似乎在工作:请参见

当您发布时,请使用代码的标记!就目前而言,它是不可读的;DR:他想在编号的行(行的开头)上拆分字符串,但不想在作为行的一部分的
(2)
上拆分字符串。