首页 > 解决方案 > 正则表达式替换多个非空行

问题描述

因此,我需要将 js 文件作为字符串读取,并将 [] 中的任何内容替换为其他数据。

原来的

const array = [
   'text_a',
   'text_b',
];

并且输出需要是

const array = [
   'replacing_text_a',
   'replacing_text_b',
   'replacing_text_c',
];

我尝试了很多东西,我得到的最接近的是

const[ ]{0,}array[ ]{0,}=[ ]{0,}\[([\s\S]{0,})];

但我明白了Unmatched ')'

标签: javascriptnode.jsregex

解决方案


使用以下正则表达式对周围进行分组

(const\s+array\s*=\s*\[)[^]*(];)

https://regex101.com/r/lDkKxd/2

https://regex101.com/r/lDkKxd/2/codegen?language=javascript


推荐阅读