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

126
Views
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 answers
Answer question

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 Report

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 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!