最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

javascript - Regular Expression for hostname & ip - Stack Overflow

programmeradmin5浏览0评论

I'm trying to find a regex to validate IP-Addresses and one for Hostnames in Javascript.

I looked at many posts here and elsewhere but cannot quite find one that suits my needs.

For the IP I found two that work fine (dont know if there are differences other than the format):

1: (this is my preferred regex for IP-Addresses)

^(?:(?: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]?)$

2:

^(([0-9]|[1-9][0-9]|1[0-9]{2}|2[0-4][0-9]|25[0-5])\.){3}([0-9]|[1-9][0-9]|1[0-9]{2}|2[0-4][0-9]|25[0-5])$

For the Hostname I found this one:

/^(([a-zA-Z0-9]|[a-zA-Z0-9][a-zA-Z0-9\-]*[a-zA-Z0-9])\.)*([A-Za-z0-9]|[A-Za-z0-9][A-Za-z0-9\-]*[A-Za-z0-9])$/

which also works fine.

BUT^^ the problem is that the hostname regex will validate

192.168.178.1111

This is not a hostname, this is an invalid IP-Address.

I would like to fit both hostname & IP regex together in a single regex term but since the hostname regex will validate any non-valid IP-Address I cannot bine them.

Does anyone have an idea on how to create a hostname regex that will not validate an invalid IP-Address?

EDIT: I found this one as well: /

but this will validate for example:

::2:3:4:5

which my application cannot accept.


Solution: thx to Aaron I have this regex for now which seems to work (in testing at the moment)

^(([a-zA-Z0-9]|[a-zA-Z0-9][a-zA-Z0-9\-]*[a-zA-Z0-9])\.)+([A-Za-z]|[A-Za-z][A-Za-z0-9\-]*[A-Za-z0-9])$

Combined version to validate IP-Addresses & Hostnames ->RegExr:

^(?:(?: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]?)$|^(([a-zA-Z0-9]|[a-zA-Z0-9][a-zA-Z0-9\-]*[a-zA-Z0-9])\.)+([A-Za-z]|[A-Za-z][A-Za-z0-9\-]*[A-Za-z0-9])$

I'm trying to find a regex to validate IP-Addresses and one for Hostnames in Javascript.

I looked at many posts here and elsewhere but cannot quite find one that suits my needs.

For the IP I found two that work fine (dont know if there are differences other than the format):

1: (this is my preferred regex for IP-Addresses)

^(?:(?: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]?)$

2:

^(([0-9]|[1-9][0-9]|1[0-9]{2}|2[0-4][0-9]|25[0-5])\.){3}([0-9]|[1-9][0-9]|1[0-9]{2}|2[0-4][0-9]|25[0-5])$

For the Hostname I found this one:

/^(([a-zA-Z0-9]|[a-zA-Z0-9][a-zA-Z0-9\-]*[a-zA-Z0-9])\.)*([A-Za-z0-9]|[A-Za-z0-9][A-Za-z0-9\-]*[A-Za-z0-9])$/

which also works fine.

BUT^^ the problem is that the hostname regex will validate

192.168.178.1111

This is not a hostname, this is an invalid IP-Address.

I would like to fit both hostname & IP regex together in a single regex term but since the hostname regex will validate any non-valid IP-Address I cannot bine them.

Does anyone have an idea on how to create a hostname regex that will not validate an invalid IP-Address?

EDIT: I found this one as well: http://jsfiddle/opd1v7au/2/

but this will validate for example:

::2:3:4:5

which my application cannot accept.


Solution: thx to Aaron I have this regex for now which seems to work (in testing at the moment)

^(([a-zA-Z0-9]|[a-zA-Z0-9][a-zA-Z0-9\-]*[a-zA-Z0-9])\.)+([A-Za-z]|[A-Za-z][A-Za-z0-9\-]*[A-Za-z0-9])$

Combined version to validate IP-Addresses & Hostnames ->RegExr.:

^(?:(?: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]?)$|^(([a-zA-Z0-9]|[a-zA-Z0-9][a-zA-Z0-9\-]*[a-zA-Z0-9])\.)+([A-Za-z]|[A-Za-z][A-Za-z0-9\-]*[A-Za-z0-9])$
Share Improve this question edited May 23, 2017 at 12:08 CommunityBot 11 silver badge asked Jul 28, 2016 at 9:11 AlkahnaAlkahna 4417 silver badges21 bronze badges 6
  • Possible duplicate of How to check validation of IP Address in jquery – Ram Singh Commented Jul 28, 2016 at 9:13
  • See this stackoverflow./questions/106179/… – Tony Vincent Commented Jul 28, 2016 at 9:13
  • those either work for IP only which I already have 2 working ones or do not work correctly on hostnames. Something like IP-Regex is shown as valid which is wrong. And you cannot bine IP & Hostname together to work correctly – Alkahna Commented Jul 28, 2016 at 9:21
  • ipv4 or ipv6 ip addresses ? ::2:3:4:5 is a valid ipv6 address (0000:0000:0000:0000:0002:0003:0004:0005) – HolyDanna Commented Jul 28, 2016 at 10:24
  • ipv4 for the moment. This may be the case but my application will not accept this kind of notation – Alkahna Commented Jul 28, 2016 at 10:32
 |  Show 1 more ment

2 Answers 2

Reset to default 3

Based on this SU answer, I propose this modification, which accepts labels starting with digits except for the top level domain :

/^(([a-zA-Z0-9]|[a-zA-Z0-9][a-zA-Z0-9\-]*[a-zA-Z0-9])\.)*([A-Za-z]|[A-Za-z][A-Za-z0-9\-]*[A-Za-z0-9])$/

Based on the AAron solution, the regex with a ma separation:

/((((([0-9]|[1-9][0-9]|1[0-9]{2}|2[0-4][0-9]|25[0-5])\.){3}([0-9]|[1-9][0-9]|1[0-9]{2}|2[0-4][0-9]|25[0-5])(\/([4-9]|[12][0-9]|3[0-2]))?)([,\s]+|$))*)|(((([a-zA-Z0-9]|[a-zA-Z0-9][a-zA-Z0-9\-]*[a-zA-Z0-9])\.)+([A-Za-z]|[A-Za-z][A-Za-z0-9\-]*[A-Za-z0-9])([,\s]+|$))*)/gm

Your opinion (see https://regex101./r/JVHcUm/1) ?

发布评论

评论列表(0)

  1. 暂无评论