Empresas
Empregos
  • Sobre nós
  • Soluções
    • Publicação de vagas
      Publique sua vaga e receba candidatos qualificados em 48h.
    • Avaliações de candidatos
      Mais de 500 testes técnicos e psicológicos, mais anti-fraude.
    • Headhunting
      Busca executiva personalizada do início ao fim.
    • Folha de Pagamento + EOR
      Dispersão de folha e EOR em mais de 15 países da LATAM.
  • Preços
  • Empregos

0

203
Visualizações
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 Respostas
Responde à pergunta

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 Relatório
Responde à pergunta
Encontrar trabalhos remotos

Descubra a nova forma de encontrar um emprego!

melhores empregos
Principais categorias de trabalho
Empresas
Postar vaga Preços Comercial
Jurídico
Termos e Condições Política de privacidade
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Recomende algumas ofertas para mim
Preciso de ajuda