如何在javascript中使用正则表达式格式化字符串
how to format the string in javascript using regular expression
这是我从数据库中获取的字符串
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.
thanks in advance.
在一个正则表达式中"计数"已经很困难了,但是你所要求的不能用一个来完成。对于之前捕获的内容,正则表达式没有任何内存,因此在第二个(2)
的情况下,您将没有任何工具来知道它已经被匹配。
现在,这里有一个有趣的工具——替换函数。你可以指定一个回调函数在这里你可以做一些检查。回调参数将是:
1)整个匹配字符串
2)捕获组(从0到n个参数)
3)匹配位置
4)初始字符串
基本上,你可以捕获你看到的数字,如果你已经看到了,什么都不做(返回相同的字符串,即参数[0]),以及其他帮助你的事情。
相关文章:
- java.net和javascript之间正则表达式的差异
- Grunt匹配正则表达式
- 不同浏览器中的空白字符正则表达式行为
- 正则表达式在字符串中找到base64
- 子字符串/正则表达式以获取字符串中保存的 SRC 值
- 调用正则表达式匹配的函数
- 使用正则表达式评估电子邮件地址时出现性能问题
- 正则表达式:使用 JavaScript 从字符串中获取格式化的数字 n.n.n
- Chrome告诉“Invaild Group”使用正则表达式来格式化数字
- 使用正则表达式重新格式化包含英国邮政编码的字符串
- Javascript - 用正则表达式替换字符串以格式化数字
- 正则表达式以格式化价格
- JavaScript - 替换/正则表达式以格式化论坛中的引号
- 格式化圣经经文引用与正则表达式在javascript
- 正则表达式,用于在 Javascript 中重新格式化美国电话号码
- 如何在javascript中使用正则表达式格式化字符串
- 使用正则表达式格式化负数
- 正则表达式格式化
- 正则表达式-格式化块中的文本- IM
- 在javascript中使用正则表达式格式化数字