用于大量替换元组语法以在 JavaScript 中导出 const 的正则表达式

Regex for mass replacement of tuple syntax to export const in JavaScript

我有很多文件具有元组语法,我想重写这些文件以升级到最新的故事书。文件如下所示:

['AcknowledgementsSection', () => <AcknowledgementsSection />],
['AuthorizedUserSection', () => <AuthorizedUserSectionWithStore />],
['AuthorizedUserAlert', () => <AuthorizedUserAlert />],

我想在 VS Code 中进行全局搜索和替换,以便输出如下所示:

export const acknowledgementsSection = () => <AcknowledgementsSection />;
export const authorizedSection = () => <AuthorizedSectionWithStore />;
export const authorizedAlert = () => <AuthorizedAlert />;

这是我目前正在尝试的:

\[('.*'), \(\) => (.*),

我如何在 VS 代码中使用它来正确替换所有内容?

更新post接受的答案

我的边缘情况是

['Decline Modal', () => <DeclineModal />],

并且需要连接捕获组

export const declineModal = () => <DeclineModal />;

一个选项是使匹配更具体一些,然后在替换中的第一个捕获组之前使用 \l

搜索

\['([^']*)', (\(\) => <[^<>]*>)\],

Regex demo

替换

export const \l ;

模式匹配

  • \[' 匹配 ['
  • ([^']*)组 1 中捕获匹配除 ' 或换行符
  • 之外的任何字符
  • ', 匹配逗号和 space
  • ( 捕获组 2
    • \(\) => <[^<>]*> 匹配 () => 然后匹配从 <>
  • ) 关闭 组 2
  • \], 匹配 ],