简体   繁体   中英

Replace double commas until there are some

I am trying to replace all double commas with ,null,

The problem is that i need to keep doing it while it is replacing it. I am thinking about adding a loop but is there any other more eficient alternative?

var test = "[1,2,,,3,4,,,,,,5,6]".replace(/,{2}/g, ",null,");

alert(test);

The result should be:

"[1,2,null,null,3,4,null,null,null,null,null,5,6]"

But is instead:

[1,2,null,,3,4,null,,null,,null,5,6]

So I would have to create a loop and do it until all double commas are done. Not sure if there is any other way?

As a side info, this is so that I can afterwards do:

var myArray = $.parseJSON(test);

Which currently it fails which I'm guessing that it's because it is not valid json.

Single regex:

"[AB,,,CD,,,,,,EF]".replace(/,(?=,)/g, ',null');

demo

Here we use the ?= lookahead to find 2 commas ("comma with a comma after it") but match and replace only the first.

Edit:

You seem to be interested in speed, here are some tests .

str.split(',').map(function(x) { return x ? x : 'null' }).join(',');

FIDDLE

splits the string by commas, then map() iterates and returns each value from the callback, and the ternary returns x (the value) if thruthy and the string 'null' if falsy, which an empty string is, then join it back together again.

Not sure if regex could handle that without looping.

Alternative solution is to split is into an array: '1,2,,,3,4,,,,,,5,6'.split(','); and then loop through it and replace all empty strings with null and then join it back.

So, something like this:

var s = '1,2,,,3,4,,,,,,5,6';
var a = s.split(',');
for (var i = 0; i < a.length; i++) {
    if (a[i] == "") {
        a[i] = "null";
    }
}
s = '[' + a.join(',') + ']';

See it here: http://jsfiddle.net/fVMLv/1/

You can do this:

var test = "[1,2,,,3,4,,,,,,5,6]".split(',').join(',|').replace(/\|,/g,"null,");
alert(test.replace(/\|/g,""));

It alerts:

[1,2,null,null,3,4,null,null,null,null,null,5,6]

Demo: http://jsfiddle.net/AmitJoki/Zuv38/

Try this

var str = "1,2,,,3,4,,,,,,5,6";
str = str.split(',');
var strResult ='';
$(str).each(function(){
    if(this==''){
        strResult +='null,';
    }
    else{
        strResult +=this+',';
    }
});
strResult = strResult.substring(0,strResult.length-1);
alert(strResult);

DEMO

The problem is with the double commas occurring consecutively. ,,,, -> will be taken as 2 sets of double commas by that RegExp. So, result will be:- ,null,,null, -> note that the occurrence of another double comma in between is skipped, since the RegEx is greedy (2nd comma is already used, which is not used again together with 3rd comma. rather 3rd and 4th are used together).

var test = "[AB,,,CD,,,,,,EF]".replace(/,,/g, ",null,").replace(/,,/g, ",null,");
alert(test);

So, with this RegExp, calling it twice will fix this.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM