Empresas
Empleos
  • Sobre nosotros
  • Soluciones
    • Publicación de vacantes
      Publica tu vacante y recibe candidatos calificados en 48h.
    • Evaluación de candidatos
      500+ pruebas técnicas y psicológicas, más anti-fraude.
    • Headhunting
      Búsqueda ejecutiva a la medida de principio a fin.
    • Nómina + EOR
      Dispersión de nómina y EOR en más de 15 países de LATAM.
  • Precios
  • Empleos

0

180
Vistas
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 Respuestas
Responde la pregunta

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 Denunciar

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 Denunciar
Responde la pregunta
Encuentra empleos remotos

¡Descubre la nueva forma de encontrar empleo!

Top de empleos
Top categorías de empleo
Empresas
Publicar vacante Precios Comercial
Legal
Términos y condiciones Política de privacidad
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Recomiéndame algunas ofertas
Necesito ayuda