Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Any recc for parsing ip + port # #35

Closed
jasikpark opened this issue Sep 7, 2022 · 2 comments
Closed

Any recc for parsing ip + port # #35

jasikpark opened this issue Sep 7, 2022 · 2 comments

Comments

@jasikpark
Copy link

jasikpark commented Sep 7, 2022

Is there any reccomendation for using this library to validate tcp addresses?

eg: 127.0.0.1:8080, 3.4.5.6:2345

I imagine I could just construct a new RegExp from this one w/

const tcpAddressRegex = new RegExp(`$${ipRegex().source}:[0-9]{0-4}^`)
@sindresorhus
Copy link
Owner

Yes, that's how I would do it. There are a few mistakes in your regex though.

const tcpAddressRegex = new RegExp(`^${ipRegex().source}:\d{1,5}$`)

@jasikpark
Copy link
Author

jasikpark commented Sep 22, 2022

Yeah, I ended up with about the same in the end:

// match an ipv6 address or `[ipv6 address]:port`
const ipv6URIRegex = new RegExp(`(?:^${ipRegex.v6().source}$)|(?:^\\[${ipRegex.v6().source}\\]:([0-9]{1,5})$)`);
// match an ipv4 address or `ipv6 address:port`
const ipv4URIRegex = new RegExp(`^(?:${ipRegex.v4().source})(?::([0-9]{1,5}))?$`);

looks like i have a typo in my comment lol

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

No branches or pull requests

2 participants