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

107
Views
JavaScript text to find the path of a link

I am using next.js and I have the following page structure, the value is stored in path

www.mysite.com/countries
www.mysite.com/countries/africa
www.mysite.com/countries/asia
www.mysite.com/countries/europe

I have sub pages under each page - value is stored in slug:

www.mysite.com/countries/africa/sudan
www.mysite.com/countries/africa/kenya
...

I want to check if slug is in the path:

slug.includes(path)

if the slug is www.mysite.com/countries/africa this also returns true for www.mysite.com/countries which I dont want.

I just want www.mysite.com/countries/africa and www.mysite.com/countries/africa/[any countries], I tried different ways but cant get this condition right.

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

0

I use regex const regex = /countries\/(.*)\/(.*)|countries\/(.*)/; to filter path = window.location.pathname. For example, I will set path manually.

You can check the below demo:

// Runing on real environment
// var path = window.location.pathname;

// Path start with /
// Uncomment to try the below example
// var path = '/countries/africa/sudan'; 
var path = '/countries/africa';

const regex = /countries\/(.*)\/(.*)|countries\/(.*)/;
var result = path.match(regex);

if (result) {

  const land = result[1] !== undefined ? result[1] : result[3];
  const country = result[2];

  if (land == 'africa') {
    if (country !== undefined) {
      // this match for www.mysite.com/countries/africa/[any countries]
      console.log('matched to country of africa URL');
    } else {
      // this match for www.mysite.com/countries/africa
      console.log('matched to africa URL');
    }
  } else {
    console.log('not matched to africa URL');
  }
} else {
  console.log('not matched');
}

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!