I'm attempting to remove special characters from my title and convert it to a url-schema. I'm able to accomplish this by using the .replace method such as: title.replace(/[^A-Za-z0-9\-/s]/g, " ");
I am running into problems when the title has parentheses in it. I am able to remove the parentheses but it then leaves an empty space at the end which then I am filling empty spaces with a -
in order to create a URL schema, this is giving me some issues.
How can I adjust my code below in order to remove the parentheses around (Cat and Dog) in order to not leave a space behind?
This is what's currently happening with my current code: "Pet Supplies Cat and Dog "
let title = "Pet Supplies (Cat and Dog)"
let cleanTitle = ""
cleanTitle = title.replace(/[^A-Za-z0-9\-/s]/g, " ");
cleanTitle = cleanTitle.toLowerCase();
cleanTitle = cleanTitle.replace(new RegExp(" ", "g"), "-");
cleanTitle = cleanTitle.replace("-and", "");
cleanTitle = cleanTitle.replace(new RegExp("---", "g"), "--");
cleanTitle = cleanTitle.replace(new RegExp("--", "g"), "-");
console.log(cleanTitle)
My expected outcome is : pet-supplies-cat-dog