在我的项目中,我试图在一个文件中找到一些单词,我用 JS 编写程序,但我有一些语法问题,我不知道为什么。目标是当程序找到“rose”时,它会在终端“flower”等上写入。 我的程序是:
var fs = require('fs');
var str = fs.readFileSync('input.txt', 'utf8');
str.split(/\s+/).forEach(s =>
console.log(
s === 'rose'
? 'flower'
: s === 'bird'
? 'animal'
: s === 'cookie'
? 'dog'
: 'unknown'
)
);
终端上出现的不同错误是:
js: "prog.js", line 5: syntax error
js: str.split(/\s+/).forEach(s =>
js: ............................^
js: "prog.js", line 6: syntax error
js: console.log(
js: ..........^
js: "prog.js", line 7: syntax error
js: s === 'rose'
js: ........^
js: "prog.js", line 9: syntax error
js: : s === 'bird'
js: .......^
js: "prog.js", line 11: syntax error
js: : s === 'cookie'
js: .......^
js: "prog.js", line 13: syntax error
js: : 'unknown'
js: .......^
js: "prog.js", line 15: syntax error
js: );
js: ^
js: "prog.js", line 1: Compilation produced 7 syntax errors.
为了运行程序,我使用了这个命令:rhino prog.js
那你能帮我找出错误吗?
最佳答案
你收到错误是因为 Rhino 当前不支持箭头函数:即 () => { ...}
您很幸运,这很容易解决!只需删除箭头功能;这应该有效:
var fs = require('fs');
var str = fs.readFileSync('input.txt', 'utf8');
str.split(/\s+/).forEach(function (s) {
return console.log(s === 'rose' ? 'flower' : s === 'bird' ? 'animal' : s ===
'cookie' ? 'dog' : 'unknown');
});
关于javascript - 我们如何在 file.txt 中找到没有 HTML 的 JavaScript 中的单词?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/52704498/