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

135
Views
Regex or alternative to strip string into variables to use in URL

I have something like this SW-763-20200622-XXX-YYY.

I need to create a URL link that has a static string + parts from the string above in a certain way - e.g. for this particular case https://www.static/SW/763/2020/06/22

So far I have something like this, that already shows static part + SW/763:

  <Link
    target="_blank"
    rel="noopener noreferrer"
    data-qa="link-report"
    href=
      {
      `https://www.static/${
        something.identifier.replace(/-/g, "/").slice(0, 6)}'
      }
  >
    Link
  </Link>

I played around with Regex and could also retrieve the year with /\d{4}(?=\d{4})/g but this approach means I would have to add to the current code, more lines with replace/regex to catch the year (with the regex I mentioned) and other(s) for the month/day also separated by / (20200622).

Wondering if there is a better way to do it either with a more complete regex or just a different approach.

about 4 years ago · Santiago Trujillo
1 answers
Answer question

0

You could capture the specific parts, and use the groups creating the expected format with replace.

^([A-Z]{2})-(\d{3})-(\d{4})(\d{2})(\d{2})\b.*

Regex demo

const regex = /^([A-Z]{2})-(\d{3})-(\d{4})(\d{2})(\d{2})\b.*/gm;
const s = `SW-763-20200622-XXX-YYY`;

console.log(s.replace(regex, `https://www.static/$1/$2/$3/$4/$5`))

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!