简体   繁体   English

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

[英]How to format using regular expression and str.replace

I am using this 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 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");

where I get output like this 我在那里得到这样的输出

18.1.1.1.1 The requirements of this chapter apply to the following: 18.1.1.1.1本章的要求适用于以下情况:

(1) New buildings or portions thereof used as health care occupancies (see 1.4.1) (1)用作医疗保健用途的新建筑物或其部分(请参阅1.4.1)

(2) Additions made to, or used as, a health care occupancy (see 4.6.6 and 18.1.1.4) (2)增加或用作卫生保健人员(见4.6.6和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 例外:18.1.1.1.1的要求不适用于被分类为除医疗保健之外的其他占用的,按照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. (2)并按照第12章至第17章和第20章至第42章的规定,满足特定入住的要求。

(3) Alterations, modernizations, or renovations of existing health care occupancies (see 4.6.7 and 18.1.1.4) (3)现有医疗服务占用的变更,现代化或整修(见4.6.7和18.1.1.4)

(4) Existing buildings or portions thereof upon change of occupancy to a health care occupancy (see 4.6.11) (4)将占用人改变为医疗占用后的现有建筑物或其部分(见4.6.11)

But my desired output is 但是我想要的输出是

18.1.1.1.1 The requirements of this chapter apply to the following: 18.1.1.1.1本章的要求适用于以下情况:

(1) New buildings or portions thereof used as health care occupancies (see 1.4.1) (1)用作医疗保健用途的新建筑物或其部分(请参阅1.4.1)

(2) Additions made to, or used as, a health care occupancy (see 4.6.6 and 18.1.1.4)(2) and conform to the requirements for the specific occupancy in accordance with Chapters 12 through 17 and Chapters 20 through 42, as appropriate. (2)对医疗人员的增加或用作(见4.6.6和18.1.1.4)(2),并符合第12章至第17章和第20章至第42章对特定人员的要求。作为适当的。

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 例外:18.1.1.1.1的要求不适用于被分类为除医疗保健之外的其他占用的,按照18.1.2.1与医疗保健住宿分开的附加设施

(3) Alterations, modernizations, or renovations of existing health care occupancies (see 4.6.7 and 18.1.1.4) (3)现有医疗服务占用的变更,现代化或整修(见4.6.7和18.1.1.4)

(4) Existing buildings or portions thereof upon change of occupancy to a health care occupancy (see 4.6.11) (4)将占用人改变为医疗占用后的现有建筑物或其部分(见4.6.11)

Here it break the line of (2) again of this value "(see 4.6.6 and 18.1.1.4) (2) " .how can i get this format? 在这里,它再次打破了(2)该值的行“(请参阅4.6.6和18.1.1.4) (2) ”。如何获得这种格式?

在您的正则表达式中,尝试仅分割\\(\\d)\\ ,在它们前面有一个空格:

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

Look for something that isn't a closing paren before the number in parens. 在括号中的数字之前查找不是结尾括号的内容。 You have to fake it out on the exception part. 您必须在例外部分伪造它。 I decided to find a non-alphabetic before the word "exception". 我决定在“例外”之前找到一个非字母顺序的字母。 That makes sure its a whole word, at least on the front. 这确保了整个单词,至少在前面。 (Though, to be fair, I can't think of an English word that ends with "expression".) (不过,公平地说,我想不出以“ expression”结尾的英语单词。)

Note how JavaScript numbers the matched strings by the ordering of the parens in the regular expression and not by the order of the parens as the matcher goes through finding things that match. 请注意,在匹配器查找匹配的事物时,JavaScript如何通过正则表达式中的括号的顺序而不是括号的顺序对匹配的字符串进行编号。 So the parts that aren't matched (because of the or) still count in the $1, $2, $3 and $4. 因此,不匹配的部分(由于或)仍计入$ 1,$ 2,$ 3和$ 4。

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

You can test Javascript regular expressions here: http://www.regexplanet.com/advanced/javascript/index.html 您可以在此处测试Javascript正则表达式: http : //www.regexplanet.com/advanced/javascript/index.html

(That's what I did to get that expression working.) (这就是我使该表达式起作用的方式。)

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

Using 使用

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

It seems working: see http://jsbin.com/otuteh/1/edit 似乎有效:请参见http://jsbin.com/otuteh/1/edit

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM