This function is for getting the link for my subcategories. Now the problem is I console.log() the values and I have the text "hire/indoor–outdoor-activities" which I don’t want to have "–" between "indoor" and "outdoor". But if I remove .replaceAll and .replace the text is "Indoor & Outdoor Activities". The problem are the two blank spaces between indoor and & and outdoor and &. Is there a regex or something so I can have only one "-"?
const subcategoryLink = (sector) => {
const sectorLink = `hire/${sector}`
const lowerCaseLink = sectorLink.toLowerCase().replace('&', '').replaceAll(' ', '-')
console.log(lowerCaseLink)
return lowerCaseLink
}
subcategoryLink("Indoor & Outdoor Activities");
>Solution :
You can go ahead and replace any sequence of spaces and ‘&’s with a single dash using regex
sectorLink.toLowerCase().replaceAll(/[ &]+/g, '-')