如何从文件node.js中的字符串中获得确切的匹配
我使自己成为一个机器人,只会从不和谐中得到一个论点,如果该论点与之匹配,它将发送“成功赎回”的信息。该部分有效,但无效的是确切的匹配。我希望它完全匹配以使该消息出现,但假设一个密钥是123456
它也将参数接受为12
或123
。我不希望这种情况发生,它应该 匹配。这是我当前的代码:
if (command === '$redeemkey') {
const fs = require('fs')
fs.readFile('notredeemed.txt', function (err, data) {
if (err) throw err;
if(data.includes(`${args[0]}`)){
console.log("true")
return message.reply({
embeds: [
new MessageEmbed()
.setColor('GREEN')
.setDescription('Redeemed Successfully')
]
})
}
这是我的noctememed.txt
的样子:
12345678
1234567
123456
12345
I made myself a bot that would simply get an argument from Discord and if the argument had matched it would send the message "Successfully Redeemed." That part works BUT what doesnt work is the exact match. I Want it to exactly match in order for that message to come up but suppose a key is 123456
it would also accept an argument as 12
or 123
. I don't want that to happen, it should EXACTLY match. Here is my current code:
if (command === '$redeemkey') {
const fs = require('fs')
fs.readFile('notredeemed.txt', function (err, data) {
if (err) throw err;
if(data.includes(`${args[0]}`)){
console.log("true")
return message.reply({
embeds: [
new MessageEmbed()
.setColor('GREEN')
.setDescription('Redeemed Successfully')
]
})
}
Here is what my notredeemed.txt
looks like:
12345678
1234567
123456
12345
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
都会在整个文件内容中检查参数。
位置,
.includes()
无论其位置哪个 /en-us/doc/web/javaScript/referent/global_objects/regexp“ rel =“ nofollow noreferrer”>regexp
为了.match()线内容。
^
- 字符串的开始$
- 字符串的结尾m
- 多线替换:
使用:
解决方案#2
您可以
替换:
用:
.includes()
would check for the argument in the entire file content no matter which position it is in.Solution #1
You could instead use
RegExp
in order to.match()
the exact value based on the line content.^
- Start of string$
- End of stringm
- Multi-lineReplace:
With:
Solution #2
You could
.split()
the line breaks in the data and then check if the created array contains the argument, similarly to how you tried checking for the value at first.Replace:
With:
您可以按线路断开代码,然后测试其平等。
使用某些
方法,如果找到匹配项,请尽早返回。文件的内容
You can split the codes by line break and then test for their equality. The
some
method is used, to return early if a match is found.contents of the file