Regular expression for validating URL in Javascript

I was looking for a decent regular expression to validate URLs that were entered as user input with.
After lots of searching on google i got the perfect solution which satisfy my requirement of my current assignment
Following is the regular expression for validating URL (Valid URL Regular Expression) a have used which worked perfectly.

^(?:http(s)?:\/\/)?[\w.-]+(?:\.[\w\.-]+)+[\w\-\._~:/?#[\]@!\$&'\(\)\*\+,;=.]+$

Following is the pattern results for the same
  • https://www.test.com   -Passed
  • http://www.test.com   -Passed
  • www.test.com   -Passed
  • test.com   -Passed
  • http://blog.test.com   -Passed
  • http://www.test.com/product   -Passed
  • http://www.test.com/products?id=1&page=2   -Passed
  • http://www.test.com#up   -Passed
  • http://255.255.255.255   -Passed
  • 255.255.255.255   -Passed
  • http://invalid.com/perl.cgi?key= | http://web-site.com/cgi-bin/perl.cgi?key1=value1&key2   -Failed
  • http://www.site.com:8008   -Passed

Comment if you like it!!

Comments

Popular posts from this blog

Use Chroma DB vector database in RAG application using llama index & Deepseek R1 local model

I have created an application to extract text from image using AI

Download DeepSeek R1 Model Locally free | AI RAG with LlamaIndex, Local Embedding and Ollama

Create Chat bot application using Python, Streamlit and Gemini AI

Restore the lost focus of Auto post back controls in asp.net update Panel control