3

How can I check if an url is made with an ip address instead of a domain name?

For example:

http://www.yeouuuuu.com/bla
https://www.bbc.co.uk/something
http://yeou:password@my.service.com/
//return true;
http://172.10.10.100/test
//return false; because there is an IP.

ip4 regex:

ipv4 = RegExp('((25[0-5]|2[0-4][0-9]|1[0-9][0-9]|[1-9][0-9]|
  [0-9])\\.){3}(25[0-5]|2[0-4][0-9]|1[0-9][0-9]|[1-9][0-9]|[0-9])');
Felix Kling
  • 795,719
  • 175
  • 1,089
  • 1,143
yeouuu
  • 1,863
  • 3
  • 15
  • 32
  • Show us how you are testing your regex against your inputs and what does not work. – Bergi May 25 '16 at 13:05
  • I don't aggree with this duplicate. I read that post before writing this. I want to know if my variable url consists of an dns name and not an ip address. – yeouuu May 25 '16 at 13:05
  • @yeouuu: The duplicate contains two regular expressions, one for a hostname and one for an IP address. It should be fairly easy for you to use these to get your answer. – Felix Kling May 25 '16 at 15:07
  • @FelixKling I think writing a regex to test all the above cases (example: http://yeou:password@my.service.com/) is a bit more complex than just knowing if it contains an ip or a domainname. And more people might benefit from the answer. – yeouuu May 25 '16 at 15:26
  • Ah I didn't realize that the other one doesn't cover user credentials. So you actually want to check more than just the domain name. You should probably point that out more explicitly in your question. – Felix Kling May 25 '16 at 15:29
  • OTOH, there are [*so many* URL regular expressions](https://stackoverflow.com/search?q=regex+valid+url) on Stack Overflow, that I don't see the value of adding yet another. It shouldn't be difficult to take any of these and adjust them to your needs. – Felix Kling May 25 '16 at 15:31
  • Well I found the answer and posted it here. I can't imagine a lot of people enjoy writing silly regexes, so it's helpfull imo. – yeouuu May 25 '16 at 15:34

4 Answers4

4

Why not just test of window.location.hostname has any letter?

window.location.hostname.match(/[a-z]/i)

If it has even one letter then it's not an IP address. No?

IMTheNachoMan
  • 5,343
  • 5
  • 40
  • 89
3

Using your regex:

function isIP( address ){ 
    r = RegExp('^http[s]?:\/\/((25[0-5]|2[0-4][0-9]|1[0-9][0-9]|[1-9][0-9]|[0-9])\\.){3}(25[0-5]|2[0-4][0-9]|1[0-9][0-9]|[1-9][0-9]|[0-9])');
    return r.test( address )
}

Returns true if the address isn't a domain name. If the URL contains an IP address after the domain, it will return false.

Griffin
  • 13,184
  • 4
  • 29
  • 43
2

This regex let's you test if your url consists of ipv4 urls

let ipv4Url = RegExp([
    '^https?:\/\/([a-z0-9\\.\\-_%]+:([a-z0-9\\.\\-_%])+?@)?',
    '((25[0-5]|2[0-4][0-9]|1[0-9][0-9]|[1-9][0-9]|[0-9])\\.){3}(25[0-5]|2[0-4',
    '][0-9]|1[0-9][0-9]|[1-9][0-9]|[0-9])?',
    '(:[0-9]+)?(\/[^\\s]*)?$'
].join(''), 'i');

ipv4Url.test('http://yeou:test@10.10.10.10/hello');
//returns true
yeouuu
  • 1,863
  • 3
  • 15
  • 32
-1

You can run your RegExp against window.location.hostname

Gilad Artzi
  • 3,034
  • 1
  • 15
  • 23