0

我有一个来自数据库的这样的字符串。

str="This is first string.Exception  No. 1: Unprotected vertical openings in accordance with 8.2.5.8 shall be permitted.Exception  No. 2:Exception  No. 1 to 8.2.5.6(1) shall not apply to patient sleeping and treatment rooms.This is test ".

我正在使用这个条件来打破新行

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

但我得到这样的输出:这是第一个字符串

Exception  No. 1: Unprotected vertical openings in accordance with 8.2.5.8 shall be permitted.

Exception  No. 2:

Exception  No. 1 to 8.2.5.6(1) shall not apply to patient sleeping and treatment rooms. 

但我想得到这样的输出: -

Exception  No. 1: Unprotected vertical openings in accordance with 8.2.5.8 shall be permitted.

Exception  No. 2:Exception  No. 1 to 8.2.5.6(1) shall not apply to patient sleeping and treatment rooms. 

这是测试。

我怎么能得到这个..提前谢谢...

4

2 回答 2

2

我建议这个:

var str = str.replace(/(Exception\s+No\.\s+\d+\:)/g, "<br /><br />$1&nbsp;");

用 the 捕捉整个起始表达式可:避免捕捉引用。请注意,我删除了i标志,它似乎没有用。

你也错过;&nbsp;.

于 2013-04-10T08:42:52.507 回答
0
var str="Exception  No. 1: Unprotected vertical openings in accordance with"+
        " 8.2.5.8 shall be permitted.Exception  No. 2:Exception  No. 1 to 8.2.5.6(1)"+
        " shall not apply to patient sleeping and treatment rooms.This is test ",
    expected = "<br /><br />Exception  No. 1: Unprotected vertical"+
               " openings in accordance with 8.2.5.8 shall be permitted."+
               "<br /><br />Exception  No. 2:Exception  No. 1 to 8.2.5.6(1)"+
               " shall not apply to patient sleeping and treatment rooms."+
               "This is test ";
str.replace(/(Exception\s+No\.\s*\d\:)/gi,"<br /><br />$1")  === expected
于 2013-04-10T08:55:25.117 回答