如何从文件node.js中的字符串中获得确切的匹配

发布于 2025-02-11 11:38:23 字数 826 浏览 1 评论 0原文

我使自己成为一个机器人,只会从不和谐中得到一个论点,如果该论点与之匹配,它将发送“成功赎回”的信息。该部分有效,但无效的是确切的匹配。我希望它完全匹配以使该消息出现,但假设一个密钥是123456它也将参数接受为12123。我不希望这种情况发生,它应该 匹配。这是我当前的代码:

    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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

风启觞 2025-02-18 11:38:23

都会在整个文件内容中检查参数。


位置,

.includes()无论其位置哪个 /en-us/doc/web/javaScript/referent/global_objects/regexp“ rel =“ nofollow noreferrer”> regexp 为了 .match()线内容。

  • ^ - 字符串的开始
  • $ - 字符串的结尾
  • m - 多线

替换:

data.includes(`${args[0]}`)

使用:

data.toString().match(new RegExp(`^${args[0]}

都会在整个文件内容中检查参数。


位置,

.includes()无论其位置哪个 /en-us/doc/web/javaScript/referent/global_objects/regexp“ rel =“ nofollow noreferrer”> regexp 为了 .match()线内容。

  • ^ - 字符串的开始
  • $ - 字符串的结尾
  • m - 多线

替换:

data.includes(`${args[0]}`)

使用:

, "m"))

解决方案#2

您可以

替换:

data.includes(`${args[0]}`)

用:

data.toString().split("\n").includes(`${args[0]}`);

.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 string
  • m - Multi-line

Replace:

data.includes(`${args[0]}`)

With:

data.toString().match(new RegExp(`^${args[0]}

.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 string
  • m - Multi-line

Replace:

data.includes(`${args[0]}`)

With:

, "m"))

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:

data.includes(`${args[0]}`)

With:

data.toString().split("\n").includes(`${args[0]}`);
离鸿 2025-02-18 11:38:23

您可以按线路断开代码,然后测试其平等。 使用某些方法,如果找到匹配项,请尽早返回。

fs.readFile('test.txt', function (err, data) {
    if (err) throw err;
    let value = args[0];

    let codes = data.toString().split(/\r?\n/);

    let result = codes.some((code) => {
        if ( code === value ) return true;
    });

    console.log(result); // for visually check whether a match was found.
    return result;
});

文件的内容

12345678
1234567
123456
12345

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.

fs.readFile('test.txt', function (err, data) {
    if (err) throw err;
    let value = args[0];

    let codes = data.toString().split(/\r?\n/);

    let result = codes.some((code) => {
        if ( code === value ) return true;
    });

    console.log(result); // for visually check whether a match was found.
    return result;
});

contents of the file

12345678
1234567
123456
12345
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文