Business
Jobs
  • About Us
  • Solutions
    • Job Postings
      Post your job and receive qualified candidates in 48h.
    • Candidate Assessments
      500+ technical and psychological tests, plus anti-fraud.
    • Headhunting
      Tailor-made executive search from start to finish.
    • Payroll + EOR
      Payroll dispersal and EOR across 15+ LATAM countries.
  • Pricing
  • Jobs

0

202
Views
Javascript regex url for domain or ip address

I am needing to check the url entered on a page and its still matching http:// only string. I want it to fail if user only enters http://

Regex pattern ^(https?:\/\/)(www\.)?(([a-zA-Z]+)?(\.[a-zA-z]{2,6})?)((\d{1,3}\.){3}(\d{1,3}){1})?$/gm

tests:

google.com  <---- fails as it should
https://google.com  <---- Pass
https:google.com  <---- fails as it should
www.cool.com.au  <---- fails as it should
https://asdsds.Com
http://a  <---- Pass
https://  <---- Pass **BUT SHOULD FAIL**
http://10.10.10.00  <---- Pass
http://www.cool.com  <---- Pass
https://123123.asd  <---- fails as it should
http://www.cool.com.au/ersdfs  <---- fails as it should
http://www.cool.com.au/ersdfs?dfd=dfgd@s=1  <---- fails as it should
http://www.cool.com:81/index.html  <---- fails as it should
about 4 years ago · Juan Pablo Isaza
1 answers
Answer question

0

The main point here is to add (?!$) after ^(https?:\/\/), it will fail the match if there is end of string/line immediately after the protocol part.

You can use

^(?:https?:\/\/)(?!$)(?:www\.)?[a-zA-Z]*(?:\.[a-zA-Z]{2,6})?(?:(?:\d{1,3}\.){3}\d{1,3})?$

See the regex demo.

Note the {1} is redundant and need removing. [a-zA-z] (that does not match only letters) is most probably a typo, you must have meant [a-zA-Z].

I removed unnecessary capturing groups and converted capturing ones into non-capturing to get cleaner regex match structure, only use capturing mechnanism when you need to actually get the match parts.

about 4 years ago · Juan Pablo Isaza Report
Answer question
Find remote jobs

Discover the new way to find a job!

Top jobs
Top job categories
Business
Post vacancy Pricing Sales
Legal
Terms and conditions Privacy policy
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Show me some job opportunities
There's an error!