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

133
Visualizações
Matching URL with username and password

I have a regex for validating IP addresses and domain names which has been working well so far

/** Checks if a URL is valid */
export function isValidURL(str: string): boolean {
  var pattern = new RegExp(
    '^(https?:\\/\\/)?' + // protocol
      '((([a-z\\d]([a-z\\d-]*[a-z\\d])*)\\.)+[a-z]{2,}|' + // domain name
      '((\\d{1,3}\\.){3}\\d{1,3}))' + // OR ip (v4) address
      '(\\:\\d+)?(\\/[-a-z\\d%_.~+]*)*' + // port and path
      '(\\?[;&a-z\\d%_.~+=-]*)?' + // query string
      '(\\#[-a-z\\d_]*)?$',
    'i'
  ); // fragment locator
  return !!pattern.test(str);
}

The problem is when I use a username and password it breaks.

http://admin:admin@192.168.92.106:5500/api/v1/

Is there a solution that would pass the URL as valid if including a username and password in the URL?

about 4 years ago · Santiago Trujillo
2 Respostas
Responde à pergunta

0

You'd just need to add a new line for the user/pass pattern after your "protocol" pattern. What are the requirements for usernames and passwords?

A simple one would looks something like this (assumes usernames are only letters & numbers):

(\w+?:.+@)?
about 4 years ago · Santiago Trujillo Relatório

0

The answer with help from @ejkeep

/** Checks if a URL is valid */
export function isValidURL(str: string): boolean {
  var pattern = new RegExp(
    '^(https?:\\/\\/)?' + // protocol
    '(\\w+?:.+@)?' + // username and password
    '((([a-z\\d]([a-z\\d-]*[a-z\\d])*)\\.)+[a-z]{2,}|' + // domain name
    '((\\d{1,3}\\.){3}\\d{1,3}))' + // OR ip (v4) address
    '(\\:\\d+)?(\\/[-a-z\\d%_.~+]*)*' + // port and path
    '(\\?[;&a-z\\d%_.~+=-]*)?' + // query string
    '(\\#[-a-z\\d_]*)?$',
    'i'
  ); // fragment locator
  return !!pattern.test(str);
}
about 4 years ago · Santiago Trujillo 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