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

156
Views
Improving Regexp performance

I have the following Regex code that's taking a long time (30+ seconds) to validate large URLS:

let validReg = 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');
let isValid = validReg.test(component.repository_url);

How can I change this code to validate the same Regex more efficiently?

about 4 years ago · Juan Pablo Isaza
1 answers
Answer question

0

You need to refactor the part where you match hyphen separated word chars: [a-z\\d]([a-z\\d-]*[a-z\\d])* => [a-z\\d]+(?:-+[a-z\\d]+)*.

Also, note you do not need to escape / chars, they are not special regex metacharacters, and you are not using a regex literal.

You may use

let validReg = new RegExp('^(https?://)?'+ // protocol
    '((([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');
let isValid = validReg.test(component.repository_url);
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!