[英]Is it possible to replace javascript require with ES6 import using a regex?
[英]With regex find and replace all require imports with ES import?
在 VS Code 中,我想使用 require 替换所有导入并更新导入路径...
import myImport = require("path/of/import");
对此
import myImport from "path/of/import";
本质上...
删除require(" ... ")
以替换为from ""
语法。
我在 VS 代码中尝试了一些工具和扩展,但它们似乎不起作用……所以只想尝试正则表达式。
我不确定这是否是您要查找的内容,但是如何匹配以下内容:
import (\w+) = require\((.*?)\);?
…并替换为以下内容:
import \1 from \2;
演示:
https://regex101.com/r/gcVbPN/1
const regex = /import (\w+) = require\((.*?)\);?/; const substitution = 'import $1 from $2;'; const before = 'import myImport = require("path/of/import");' const after = before.replace(regex, substitution); console.log(`${before}\n${after}`);
您是否尝试永久更改导入? 如,您现在希望您的导入是import MyImport from "path/of/import";
你不需要import myImport = require("path/of/import");
? 如果是这样,您可能会使用 VS Code 的搜索工具 (ctrl+shift+F),您可以在其中搜索文本字符串并在整个目录中替换它们。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.