Business
Jobs
  • About Us
  • Solutions
    • Job Postings
      Post your job and receive qualified candidates in 48h.
    • Candidate Assessments
      500+ technical and psychological tests, plus anti-fraud.
    • Headhunting
      Tailor-made executive search from start to finish.
    • Payroll + EOR
      Payroll dispersal and EOR across 15+ LATAM countries.
  • Pricing
  • Jobs

0

175
Views
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 answers
Answer question

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 Report

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 Report
Answer question
Find remote jobs

Discover the new way to find a job!

Top jobs
Top job categories
Business
Post vacancy Pricing Sales
Legal
Terms and conditions Privacy policy
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Show me some job opportunities
There's an error!