I have to get keywords from the client and need to remove all spaces next to commas.
I have the following keywords list style
,air duster, apple, samsung , power bank station,sony
I need like this
air duster,apple,samsung,power bank station,sony
my code
$("#my-input").on("focusout", function() {
console.log("FOCUS OUT")
$(this).attr("value",$(this).val().split(/[ ,]+/).join(',').trim());
})
,air,duster,apple,samsung,power,bank,station,sony
2
Answers
You can use
Here,
.split(",")
– splits with a comma.map(x => x.trim())
– removes leading/trailing whitespaces.filter(Boolean)
– removes empty items.join(",")
– join the items with a single comma.