1. Advertising
    y u no do it?

    Advertising (learn more)

    Advertise virtually anything here, with CPM banner ads, CPM email ads and CPC contextual links. You can target relevant areas of the site and show ads based on geographical location of the user if you wish.

    Starts at just $1 per CPM or $0.10 per CPC.

Creating a regex to get multiple values and print

Discussion in 'JavaScript' started by Private Loader, Jan 12, 2021.

  1. #1
    I'm looking for any help with the code below. Basically the ip:port string keeps changing and may contain more than one true value in str and i wonder if is possible to use a regex using the search function to get the all ip:port and print in demo.

    
    <p id="demo"></p>
    
    <script>
    var str = "Please 127.0.0.1:80 locate where 127.0.0.2:80 occurs!";
    var pos = str.search("\b(?:(?:25[0-5]|2[0-4][0-9]|[01]?[0-9][0-9]?)\.){3}(?:25[0-5]|2[0-4][0-9]|[01]?[0-9][0-9]?):\d{1,5}\b");
    document.getElementById("demo").innerHTML = pos;
    </script>
    
    Code (markup):
    Thanks
     
    Private Loader, Jan 12, 2021 IP
  2. iago111

    iago111 Member

    Messages:
    99
    Likes Received:
    4
    Best Answers:
    1
    Trophy Points:
    33
    #2
    So you have a random text with IPs+port in that text and you want to extract all IP+port within that text, right?
     
    iago111, Jan 14, 2021 IP
  3. Private Loader

    Private Loader Active Member

    Messages:
    103
    Likes Received:
    8
    Best Answers:
    0
    Trophy Points:
    58
    #3
    @iago111 - Thanks. That is exactly what i'm looking for.
     
    Private Loader, Jan 20, 2021 IP
  4. ActiveFrost

    ActiveFrost Notable Member

    Messages:
    2,072
    Likes Received:
    63
    Best Answers:
    3
    Trophy Points:
    245
    #4
    console.log(
      str.match(/([1-9]|[0-9]|[0-9]).([0-9]|[0-9]|[0-9]).([0-9]|[0-9]|[0-9]).([0-9]|[0-9]|[0-9]):[0-9]+/g)
    );
    Code (JavaScript):
    Output:
    [
    "127.0.0.1:80",
    "127.0.0.2:80"
    ]
    Code (markup):
     
    ActiveFrost, Jan 21, 2021 IP
  5. Private Loader

    Private Loader Active Member

    Messages:
    103
    Likes Received:
    8
    Best Answers:
    0
    Trophy Points:
    58
    #5
    @ActiveFrost - thanks for your reply...

    After testing i found out that there is some issues with your code.

    - You changed the regex to match ips but on the console is giving
    ["7.0.0.1:80", "7.0.0.2:80"] when it should be ["127.0.0.1:80","127.0.0.2:80"]

    - Something i have been searching for a while...
    How to print the text generated in console.log() on the page?
     
    Last edited: Jan 21, 2021
    Private Loader, Jan 21, 2021 IP
  6. ActiveFrost

    ActiveFrost Notable Member

    Messages:
    2,072
    Likes Received:
    63
    Best Answers:
    3
    Trophy Points:
    245
    #6
    Sorry, messed up a bit with the OR operator. This (see below) does the trick.
    <div id="output">
    
    </div>
    HTML:
    var str = "Please 127.0.0.1:80 locate where 127.0.0.2:80 occurs!";
    
    var result = str.match(/([0-9]?[0-9]?[0-9]).([0-9]?[0-9]?[0-9]).([0-9]?[0-9]?[0-9]).([0-9]?[0-9]?[0-9]):[0-9]+/g);
    
    for (i = 0; i < result.length; i++) {
        document.getElementById("output").innerHTML += result[i] + '<br>';
    }
    Code (JavaScript):
     
    ActiveFrost, Jan 21, 2021 IP
  7. Private Loader

    Private Loader Active Member

    Messages:
    103
    Likes Received:
    8
    Best Answers:
    0
    Trophy Points:
    58
    #7
    @ActiveFrost - Thanks for the code.

    I have another question on same subject. The code above seems to be clear and after testing, it works.
    The problem is when i tried to include with my script i got a error.

    Basically the script that i'm building gets a rss feed, convert to json and from there the code below is executed in a for loop...
    
    for (x in myObj.items) {
    
    // This line should print the demo
    document.write('<div id="demo"></div>');
    
    // This lines should read the obj description from the feed and match the ip:port regex.
    var str = ''+myObj.items[x].description+'';
    var result = str.match(/([0-9]?[0-9]?[0-9]).([0-9]?[0-9]?[0-9]).([0-9]?[0-9]?[0-9]).([0-9]?[0-9]?[0-9]):[0-9]+/g);
    for (i = 0; i < result.length; i++) {
    document.getElementById("demo").innerHTML += result[i] + "<br>";
    }
    }
    
    Code (markup):
    The problem is the for loops, when using both it doesn't work. The idea is to get 10 feed results, read the description of each filter and print in the screen.

    Thanks
     
    Private Loader, Jan 22, 2021 IP