Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

Problem I get unwanted things with .replaceall

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 :

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

You can go ahead and replace any sequence of spaces and ‘&’s with a single dash using regex

sectorLink.toLowerCase().replaceAll(/[ &]+/g, '-')
Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading