I have an input field where the user is only allowed to use letters, spaces and commas.
I've created this here so far:
splitStr = splitStr.split(' ').join(', ');
splitStr = splitStr.split(',').join(', ');
splitStr = splitStr.split(';').join(', ');
splitStr = splitStr.split('-').join(', ');
splitStr = splitStr.split('_').join(', ');
splitStr = splitStr.split('/').join(', ');
splitStr = splitStr.split('#').join(', ');
$("imgTags").value = splitStr;
// removes duplicate spaces
splitStr = splitStr.replace(/ +(?= )/g,'');
// removes duplicate commas
splitStr = splitStr.replace(/,+/g,',');
// missing: remove ', ' duplicates
So this code above makes it so that the users input is always converted to a comma space and on the bottom of the code I'm removing artifacts that can happen, like duplicate commas or duplicate spaces.
In the first like you can see that I'm also replacing any space with comma space.. this gives me an artifact of , , , , , , , , , , , , , , , this means I need also to replaces any comma space comma space with just a single comma space, so I've tried to do this but I never get the the desired result.
How can I replace regex for space comma duplicates?
This: , , , , , , needs to become this , e.g. comma space comma space comma space needs to be just comma space.
[ ,;_\/#-]+and repeat that 1+ times and then replace with a comma space regex101.com/r/tUU24v/1