I need a regrex to replace commas are are between two characters that are no spaces.
Text: HOMER, Simpson,JACK, Daniels,NICK, Cage
Desired outcome: HOMER, Simpson - JACK, Daniels - NICK, Cage
This is what I could come up with but it replaces the letters as well as the comma
/[a-zA-z],[a-zA-z]/
"HOMER, Simpson,JACK, Daniels,NICK, Cage".replace(/(,(?!\s))/g, ' - ');
http://jsfiddle.net/samliew/sQKNN/
If you need to also check for leading space before a comma,
.replace(/((?!\s),(?!\s))/g, ' - '))
In regex, \S
represents all non-space characters.
var input = "HOMER, Simpson,JACK, Daniels,NICK, Cage";
var output = input.replace(/(\S),(\S)/g, '$1 - $2');
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With