Replace a comma with a comma in single quotes in a string [duplicate]

2

In a nutshell, what I need is to substitute a comma with a comma with a comma between two single quotes.

The result you give me does not include single quotes so far.

This is the JS code:

var Cadena = "23,54,N21,98,BIT";
var input = Cadena.trim();

input.replace(",", "\', \'");

var output = input.slice(0, -1);

console.log(output);
    
asked by Eduardorq 31.03.2017 в 14:20
source

1 answer

6

You have two problems, the first is that the replace only replaces the first occurrence. The next problem is that replace changes the value but does not change the variable you are using, so you have to assign it again.

In summary, what you need would stay like this:

String.prototype.replaceAll = function(search, replacement) {
    var target = this;
    return target.replace(new RegExp(search, 'g'), replacement);
};

var Cadena = "23,54,N21,98,BIT";
var input = Cadena.trim();

input=input.replaceAll(",", "\', \'");

var output = input.slice(0, -1);

console.log(output);
    
answered by 31.03.2017 / 14:26
source