-1

I'm wanting to scan a message/string for an IP address, I've managed to get it to check if the whole thing matches but I cannot get it to check if it contains an IP

Here's what I've tried

let ipRegex = /^(25[0-5]|2[0-4][0-9]|[01]?[0-9][0-9]?)\.(25[0-5]|2[0-4][0-9]|[01]?[0-9][0-9]?)\.(25[0-5]|2[0-4][0-9]|[01]?[0-9][0-9]?)\.(25[0-5]|2[0-4][0-9]|[01]?[0-9][0-9]?)$/;

  if(message.content.match(ipRegex)) {
    client.sql.query(`SELECT autoDelIPs, modlog FROM guildconfig WHERE guildID = ?`, [message.guild.id], async function (error, results, fields) {
      if(results[0].autoDelIPs === 1) {
        if(results[0].modlog !== "0" || results[0].modlog !== "000000000000000000") {
          let logemb = new Discord.MessageEmbed().setTitle("IP Detected").addField("User", message.author.tag, true).addField("Sent", message.content, true).addField("In", "<#" + message.channel.id + ">", true).setColor("RED").addField("Action Taken", "Message deleted", true)
          message.guild.channels.cache.get(results[0].modlog).send(logemb)
          message.delete()
          message.channel.send("An IP was detected to be sent by " + message.author.tag + " and was deleted, a log has been sent into the modlog channel!")
        } else {
          message.delete()
          message.channel.send("An IP was detected to be sent by " + message.author.tag + " and was deleted, no log has been made as a modlog was not set!")
        }
      }
    })
  }

Using this code here are some output examples:

Message: Look at my ip 1.1.1.1 lol

Output: Nothing happens

Message: 8.8.8.8

Output: An IP was detected to be sent

I have tried looking for myself for an answer but could not find any, I know it might be a stupid question but I can't find any answers to my problem

Syntle
  • 5,168
  • 3
  • 13
  • 34
YuriDev
  • 29
  • 4

1 Answers1

5

In regular expressions ^ and $ mean start and end of the line. So, your expression would only capture the IP if it is the only thing in the message.

Remove them and it will work as expected.

I recommend playing with your regular expressions o RegExr as it has a tool for explaining the payload.

Mihail Feraru
  • 1,419
  • 9
  • 17
  • 2
    And add a `g` on the end if you want to find *all* addresses: `/(25[0-5]|2[0-4][0-9]|[01]?[0-9][0-9]?)\.(25[0-5]|2[0-4][0-9]|[01]?[0-9][0-9]?)\.(25[0-5]|2[0-4][0-9]|[01]?[0-9][0-9]?)\.(25[0-5]|2[0-4][0-9]|[01]?[0-9][0-9]?)/g` – dwb Aug 02 '20 at 19:17