I'm looking for any help with the code below. Basically the iport 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 iport 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
So you have a random text with IPs+port in that text and you want to extract all IP+port within that text, right?
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 - 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?
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 - 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