Empresas
Empregos
  • Sobre nós
  • Soluções
    • Publicação de vagas
      Publique sua vaga e receba candidatos qualificados em 48h.
    • Avaliações de candidatos
      Mais de 500 testes técnicos e psicológicos, mais anti-fraude.
    • Headhunting
      Busca executiva personalizada do início ao fim.
    • Folha de Pagamento + EOR
      Dispersão de folha e EOR em mais de 15 países da LATAM.
  • Preços
  • Empregos

0

183
Visualizações
How to get an EXACT match from a string inside a file Node.js

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
about 4 years ago · Juan Pablo Isaza
2 Respostas
Responde à pergunta

0

.contains() 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]}$`, "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]}`);
about 4 years ago · Juan Pablo Isaza Relatório

0

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('notredeemed.txt', function (err, data) {
    if (err) throw err;
    let value = args[0];

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

    codes.some((code) => {
        if ( result === value ) return true;
    });
});
about 4 years ago · Juan Pablo Isaza Relatório
Responde à pergunta
Encontrar trabalhos remotos

Descubra a nova forma de encontrar um emprego!

melhores empregos
Principais categorias de trabalho
Empresas
Postar vaga Preços Comercial
Jurídico
Termos e Condições Política de privacidade
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Recomende algumas ofertas para mim
Preciso de ajuda