简体   繁体   中英

Unable to loop through a string to find regex pattern number

I'm trying to make an app where a client will search some string in a textarea and that string will be looped through to find an IP address. If it finds an IP then it will take its value and use it in an API to request for more data. The string, which the client searches will always have an IP. But I'm not getting an ip. Here's the code:

HTML

<div class="row">
        <div class="col-sm-12 mb-5">
                <form action="/finder" method="GET" id="ipForm">
                        <textarea type="text" name="header" id="form-input" class="form-control form-input form-inline justify-content-center" required></textarea>
                        <input type="submit" class="btn btn-primary" id="form-submit" value="CHECK">
                </form>
        </div>
</div>

Router

router.get("/finder", (req, res) => {
    if(req.query.header) {
        var query = req.query.header;
        var rawQuery = query.split("\n");

        for(var i = 0; i < rawQuery.length; i++) {
            var ip = /Received:\s+from.*?\[((?:[0-9]{1,3}\.){3}[0-9]{1,3})\]/i.exec(rawQuery[i]);

            if(ip !== null) {
                var url = "https://api.ipregistry.co/" + ip;
                request(url, function(error, response, body) {

                    if(!error && response.statusCode === 200) {
                        const data = JSON.parse(body);
                        res.render("../views/finder", {data: data});
                    }       
                });
            }else{
                // var data = "Unable to retrieve data.";
                // res.render("../views/finder", {data: data});
                console.log("no ip found");
            }
        }
    }else{
        res.render("../views/finder", {data: null});
    }
});

Edited with more info - input value will be something like this

Return-path: <user@example.com>
Received: from mac.com ([10.13.11.252])
  by ms031.mac.com (Sun Java System Messaging Server 6.2-8.04 (built Feb 28
  2007)) with ESMTP id <0JMI007ZN7PETGC0@ms031.mac.com> for user@example.com; Thu,
  09 Aug 2007 04:24:50 -0700 (PDT)
Received: from mail.dsis.net (mail.dsis.net [70.183.59.5])
  by mac.com (Xserve/smtpin22/MantshX 4.0) with ESMTP id l79BOnNS000101
  for <user@example.com>; Thu, 09 Aug 2007 04:24:49 -0700 (PDT)
Received: from [192.168.2.77] (70.183.59.6) by mail.dsis.net with ESMTP
  (EIMS X 3.3.2) for <user@example.com>; Thu, 09 Aug 2007 04:24:49 -0700
Date: Thu, 09 Aug 2007 04:24:57 -0700
From: Frank Sender <sender@example.com>
Subject: Test
To: Joe User <user@example.com>
Message-id: <61086DBD-252B-46D2-A54C-263FE5E02B41@example.com>
MIME-version: 1.0 (Apple Message framework v752.2)
X-Mailer: Apple Mail (2.752.2)
Content-type: text/plain; charset=US-ASCII; format=flowed
Content-transfer-encoding: 7bit

This pattern (?:[0-9]{1,3}\.){3}[0-9]{1,3} returns an IP for sure. If you expecting multiple Ips you may just want to use /(?:[0-9]{1,3}\.){3}[0-9]{1,3}/g instead of /(?:[0-9]{1,3}\.){3}[0-9]{1,3}/i to search globally instead of looping through sentences.

In your question you are adding extra regular expression to search for "Received from IP_ADDRESS" and those extra text has to match as well. Try to match the IP without it.

I tried to play with your data and the way you are trying to extract ip from your data. Here is an screenshot of it. So, it seems like your regex returns an array. Try to console.log the ip variable and check what it contains. In my experiment, to access the ip address I had to do this ip[1] .

在此处输入图像描述

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM