Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/javascript/464.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
如何使用正则表达式在javascript中格式化字符串_Javascript_Html_Regex - Fatal编程技术网

如何使用正则表达式在javascript中格式化字符串

如何使用正则表达式在javascript中格式化字符串,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 usedas, a health care occupancy (see 4.6.6 and 18.1.1.4)(2)

这是我从数据库中得到的字符串

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.
提前感谢..

在正则表达式中“计数”已经很难了,但是你所要求的不能用一个来完成。正则表达式没有以前捕获的任何内存,因此在第二个
(2)
的情况下,您将没有任何工具知道它已被匹配

现在,这里有一个有趣的replace函数工具。你可以指定一个回调函数,在这里你可以做一些检查。回调参数将为:
1) 整个匹配字符串
2) 捕获组(从0到n个参数)
3) 匹配的位置
4) 初始字符串


因此,基本上,您可以捕获您看到的数字,如果您已经看到了,则什么也不做(返回相同的字符串,即参数[0]),还有其他一些东西可以帮助你……

我认为如果你能在自己动手解决这个问题后,展示出你正在努力解决的问题的哪一部分会更好。你错了什么还是什么?你已经问了16个问题。是时候把更多的精力放在正确格式化你的文章上了。请用文字解释你想要正则表达式做什么。这些例子本身还不够清楚。你可以看到,当它得到(0到9)或“异常”时,它会跳转到新行。但是在句子的中间也有(0—9)和“例外”字,不需要打断这行。但是我的正则表达式只是把它打断到新行。
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.