Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/javascript/375.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
如何替换javascript项目中的所有符号名_Javascript_Parsing - Fatal编程技术网

如何替换javascript项目中的所有符号名

如何替换javascript项目中的所有符号名,javascript,parsing,Javascript,Parsing,我想写一个自动(使用一个规则系统和一个用作替换的单词列表)的东西,用其他东西替换所有变量和函数名: 例如: 致: 我已经在网上搜索过了,但还没有找到任何可以轻松修改的项目 你们中有谁知道如何做这件事,或者知道一个我可以作为起点的项目吗?你们是在找工作还是喜欢做什么 您可以编写一个简短的Shell脚本(例如bash)。对于循环和包含单词列表的三个变量,您需要sed和。如果您在名为my_first_code.txt的文件中有您的第一个带有脚、猫和状态的代码,那么它将如下所示: foot_words=

我想写一个自动(使用一个规则系统和一个用作替换的单词列表)的东西,用其他东西替换所有变量和函数名:

例如:

致:

我已经在网上搜索过了,但还没有找到任何可以轻松修改的项目

你们中有谁知道如何做这件事,或者知道一个我可以作为起点的项目吗?

你们是在找工作还是喜欢做什么


您可以编写一个简短的Shell脚本(例如bash)。对于循环和包含单词列表的三个变量,您需要
sed
。如果您在名为
my_first_code.txt
的文件中有您的第一个带有脚、猫和状态的代码,那么它将如下所示:

foot_words="house ba be bi bo bu"
cat_words="bear da de di do du"
state_words="country ka ke ki ko ku"

count=1
for word in $foot_words; do
    sed 's%foot%'$word'%g' my_first_code.txt > new_code_$count.txt
    ((count++))
done
count=1
for word in $cat_words; do
    sed -i 's%cat%'$word'%g' new_code_$count.txt
    ((count++))
done
count=1
for word in $state_words; do
    sed -i 's%state%'$word'%g' new_code_$count.txt
    ((count++))
done
在本例中,您将获得6个新文件
new\u code\u 1.txt
new\u code\u 2.txt
new\u code\u 3.txt
,依此类推


解释:for
循环的第一个
复制
my\u first\u code.txt
中的代码,并用新词替换单词foot。另外两个
for
循环只替换新文件中的单词。

谷歌闭包编译器()能够识别函数名和变量名,但没有内置选项来替换为您选择的名称

因为直接查找和替换没有上下文,如果你想自己使用,你需要使用正则表达式来“解析”JavaScript,如果你使用递归正则表达式(如.NET中的带平衡组的正则表达式),这很困难,但可以管理


您希望这是运行时还是构建时?这是用于ide的吗?有点像智能查找/替换可能是一个使用
sed
grep
的shell脚本?记事本++中的“查找和替换”命令会产生奇迹:)它不像替换单词那么简单。您将需要编写一个复杂的进程,该进程可以读取Javascript并区分用户定义的函数名和变量名。我曾考虑为此使用模糊器,但我发现没有一个模糊器允许我控制将替换哪些名称。在这种情况下,我更希望在中打开代码,然后右键单击函数/变量定义重构->重命名
var house = 0;
function bear(country) {
    return country ? "running" : "sleep";
}
bear(house);
foot_words="house ba be bi bo bu"
cat_words="bear da de di do du"
state_words="country ka ke ki ko ku"

count=1
for word in $foot_words; do
    sed 's%foot%'$word'%g' my_first_code.txt > new_code_$count.txt
    ((count++))
done
count=1
for word in $cat_words; do
    sed -i 's%cat%'$word'%g' new_code_$count.txt
    ((count++))
done
count=1
for word in $state_words; do
    sed -i 's%state%'$word'%g' new_code_$count.txt
    ((count++))
done