You can do this only with javascript:
function arranjar(str){
var partes = str.split(',');
var ultima = partes.pop();
return [partes.join(''), ultima].join(',');
}
arranjar('123,345,678,abc,qualquer,coisa') // dá "123345678abcqualquer,coisa"
To do this with regex is no longer simple. But an example would be like this:
function arranjarComRegex(str){
var partes = str.match(/(.*),(.*)/);
var string = partes[1].replace(/,/g, '');
return string + ',' + partes[2];
}
In this case the regex (.*),(.*)
captures two groups. On the first catch '123,345,678,abc,qualquer'
where you need to remove the commas with .replace(/,/g, '')
and the second capture group gets the last word, which then needs to join back in the string with string + ',' + partes[2]
.
is that actually, I wanted to know more on how to do this with regex
– Lai32290
@Lai32290 ok, I’ll add an example with regex.
– Sergio