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.
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.*
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`))