Skip to content

Conversation

@slagiewka
Copy link
Contributor

This is my first approach to the problem to see what you think about it. If it's beyond what you're willing to cover, I'll do another PR with covering :: only.


This now covers cases such as the unspecified IPv6 address, NAT64
mapping as well as scoped and zoned IPv6 addresses.

It's worth to mention that validator.js has also moved to the same approach.

Closes: #238

This now covers cases such as the unspecified IPv6 address, NAT64
mapping as well as scoped and zoned IPv6 addresses.

Closes: af#238
Copy link
Owner

@af af left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thanks for the PR and the added tests! Suggested a simplified version which I think I prefer, let me know if there are any gaps this doesn't account for

`(?:${v6Seg}:){2}(?:(?::${v6Seg}){0,3}:${v4Str}|(?::${v6Seg}){1,5}|:)|` +
`(?:${v6Seg}:){1}(?:(?::${v6Seg}){0,4}:${v4Str}|(?::${v6Seg}){1,6}|:)|` +
`(?::(?:(?::${v6Seg}){0,5}:${v4Str}|(?::${v6Seg}){1,7}|:))` +
')(?:%[0-9a-zA-Z-.:]{1,})?$');
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

What do you think about this implementation instead? It still passes all tests and is a bit less code. Your version is laid out better, but I'd argue that both are well into "write only" mode for most of us regex mortals :)

// "best effort" regex-based IP address check
// If you want a more exhaustive check, create your own custom validator
const ipv4Regex = /^(25[0-5]|2[0-4][0-9]|1[0-9][0-9]|[1-9][0-9]|[0-9])(\.(25[0-5]|2[0-4][0-9]|1[0-9][0-9]|[1-9][0-9]|[0-9])){3}$/
const ipv6Regex = /^(([0-9a-f]{1,4}:){7}[0-9a-f]{1,4}|([0-9a-f]{1,4}:)+:([0-9a-f]{1,4}:)*[0-9a-f]{1,4}|([0-9a-f]{1,4}:)*::([0-9a-f]{1,4}:)*[0-9a-f]{0,4}|::|(([0-9a-f]{1,4}:){1,6}|:):((25[0-5]|2[0-4][0-9]|1[0-9]{2}|[1-9]?[0-9])\.){3}(25[0-5]|2[0-4][0-9]|1[0-9]{2}|[1-9]?[0-9]))(%[0-9a-z.\-:]+)?$/i

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This seems fine, too 😉 I'm not sure we're covering ALL specs possible, but it's definitely an improvement that I appreciate 🙂

You might want to consider reusing these segments as in the standard library example. Might be a bit easier on the eyes 😉

@af af merged commit 061b888 into af:main Oct 17, 2025
4 checks passed
@slagiewka slagiewka deleted the improve_ip_regex branch October 23, 2025 08:09
@slagiewka
Copy link
Contributor Author

Hey @af, do you have any plans to release it soon?

@af
Copy link
Owner

af commented Dec 1, 2025

@slagiewka v8.1.1 is out and has this change in it

@slagiewka
Copy link
Contributor Author

Oh shoot! npm was actually the last place I did go to looking for the release.

I actually saw nothing in GH Releases nor any git tag for it. Thanks anyway :)

@af
Copy link
Owner

af commented Dec 2, 2025

Ah! I forgot to git push --tags, sorry about that!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

Support the unspecified IPv6 address in host

2 participants