简体   繁体   English

无法遍历字符串以查找正则表达式模式编号

[英]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.我正在尝试制作一个应用程序,客户端将在文本区域中搜索一些字符串,并且该字符串将被循环以查找 IP 地址。 If it finds an IP then it will take its value and use it in an API to request for more data.如果它找到 IP,那么它将获取它的值并在 API 中使用它来请求更多数据。 The string, which the client searches will always have an IP.客户端搜索的字符串将始终具有 IP。 But I'm not getting an ip.但我没有得到 ip。 Here's the code:这是代码:

HTML 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.这种模式(?:[0-9]{1,3}\.){3}[0-9]{1,3}肯定会返回 IP。 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.如果您期望多个 Ips,您可能只想使用/(?:[0-9]{1,3}\.){3}[0-9]{1,3}/g而不是/(?:[0-9]{1,3}\.){3}[0-9]{1,3}/i进行全局搜索,而不是遍历句子。

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.在您的问题中,您添加了额外的正则表达式来搜索“从 IP_ADDRESS 接收”,并且这些额外的文本也必须匹配。 Try to match the IP without it.尝试在没有它的情况下匹配 IP。

I tried to play with your data and the way you are trying to extract ip from your data.我尝试使用您的数据以及您尝试从数据中提取 ip 的方式。 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.尝试console.log ip变量并检查它包含的内容。 In my experiment, to access the ip address I had to do this ip[1] .在我的实验中,要访问 ip 地址,我必须这样做ip[1]

在此处输入图像描述

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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