首页 > 解决方案 > 用于使用环视删除空行的正则表达式

问题描述

标签: javascriptnode.jsregextypescript

解决方案


您不需要断言,因为它们是一次性使用的。
您不能在全局上下文中使用它们,因为您
在其中使用了两个锚点^$

因此,它们是无用的。
要区分换行符,只需添加捕获组。
另外,不要在字符类中使用 Unicode 字符,你可能
有一天会犯错。

就像我会做的那样......

/^[\u300c\u300e][^\r\n]+[\u300f\u300d](\s*\r?\n\s*\r?\n\s*)[\u300c\u300e][^\r\n]+[\u300f\u300d]$/

展开

 ^ 
 [\u300c\u300e] 
 [^\r\n]+ 
 [\u300f\u300d] 
 (                             # (1 start)
      \s* 
      \r? \n \s* 
      \r? \n \s* 
 )                             # (1 end)
 [\u300c\u300e] 
 [^\r\n]+ 
 [\u300f\u300d] 
 $ 

推荐阅读