Extract ip address and port from a string in javascript

javascript

Solution

`window.location.hostname` and `window.location.port`

Read more about `window.location` at https://developer.mozilla.org/en-US/docs/Web/API/window.location

Problem

In my client side code, the user enter an address like: ``` http://192.168.1.52:8080/home/client.html ``` I would like to extract the ip address 192.168.1.52 and the port number 8080 separately. Here is what I did to extract the ip, but failed! ``` var url = window.location.href; ValidIpAddressRegex = new RegExp("^(25[0-5]|2[0-4]\\d|[0-1]?\\d?\\d)(\\.(25[0-5]|2[0-4]\\d|[0-1]?\\d?\\d)){3}$"); var ip= ValidIpAddressRegex.exec(url); alert(ip); ``` What would be the correct way of extracting IP and port number in javascript.- Thank you

Original source