I need to split a keyword string and turn it into a comma delimited string. However, I need to get rid of extra spaces and any commas that the user has already input.
var keywordString = ford tempo, with,,, sunroof;
Output to this string:
ford,tempo,with,sunroof,
I need the trailing comma and no spaces in the final output.
Not sure if I should go Regex or a string splitting function.
Anyone do something like this already?
I need to use javascript (or JQ).
EDIT (working solution):
var keywordString = , ,, ford, tempo, with,,, sunroof,, ,;
//remove all commas; remove preceeding and trailing spaces; replace spaces with comma
str1 = keywordString.replace(/,/g , '').replace(/^ss*/, '').replace(/ss*$/, '').replace(/[s,]+/g, ',');
//add a comma at the end
str1 = str1 + ',';
console.log(str1);