我有一个来自数据库的类似字符串。
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 ");
但我得到这样的输出: 这是第一个字符串
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.
这是测试。
我怎样才能得到这个......提前致谢......
最佳答案
我建议这个:
var str = str.replace(/(Exception\s+No\.\s+\d+\:)/g, "<br /><br />$1 ");
使用 :
捕获整个起始表达式可以避免捕获引用。请注意,我删除了 i
标志,它似乎没有用。
您还缺少
末尾的 ;
。
关于javascript - 如何使用正则表达式和javascript控制数据,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/15920837/