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

Validate a URL #358

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open

Validate a URL #358

wants to merge 1 commit into from

Conversation

rajlomror
Copy link

One liner JavaScript function to check whether a given value is a valid URL or not.

Copy link
Contributor

@elkarouani elkarouani left a comment

Choose a reason for hiding this comment

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

Can you add an explanation of each part of that line of code above the code ?

@robinpokorny
Copy link
Contributor

Hey, @rajlomror, thanks for your contribution! However, I have some doubts about this RegExp. Did you write it yourself or copied from somewhere? Why are there some IPs separate? It seems that the use of this is for something else (like figuring if the URL might be on the local network).

In general, it's very thicky to write regexps for such general concepts. Similar to an email, the result is either super complex and opinionated or trivial. Maybe it would be better to figure out how to take advantage of build-in URL 😉

@elkarouani
Copy link
Contributor

This solution make use of the URL native API :
const isUrl = string => { try { return Boolean(new URL(string)); } catch(e){ return false; } }

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.

None yet

3 participants