Replacing empty values with nulls in JavaScript string

Given the following string:

var str = "[,,,1,2,,,3,4,,,,,,5,6]";

I want to replace all "empty" values with nulls. In other words, I want this result:

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

This almost works, but it misses the first empty value:

var str = "[,,,1,2,,,3,4,,,,,,5,6]";
str.split(',').map(function(x) { return x ? x : 'null' }).join(',')
// Gives [,null,null,1,2,null,null,3,4,null,null,null,null,null,5,6]

Likewise, I noticed if I have trailing empty values, it misses the last one there too:

var str = "[,,,1,2,,,3,4,,,,,,5,6,,]";
str.split(',').map(function(x) { return x ? x : 'null' }).join(',')
// Gives [,null,null,1,2,null,null,3,4,null,null,null,null,null,5,6,null,]

Any ideas how I can make sure that first and last empty values are also replaced?

Thanks!

6 answers

  • answered 2018-02-13 03:15 xianshenglu

    this is going to work:

    	 var str = "[,,,1,2,,,3,4,,,,,,5,6]";
    	 var result = '[' + str.replace(/^\[|\]$/g, '').split(/[,]/).map(function(x) { return x ? x : 'null' }).join(',') + ']';
    	 console.log(result);

  • answered 2018-02-13 03:16 Shanon Jackson

    You have removed all the empty values. "[" and "]" are not nothing.

  • answered 2018-02-13 03:16 Obsidian Age

    The secret is that your brackets are getting combined with your commas / numbers at the start and end of the string when it is split. In order to resolve this, you need to remove the brackets before splitting.

    This can be done with .replace() on /[\][]/g:

    var str = "[,,,1,2,,,3,4,,,,,,5,6,]";
    console.log(
      str.replace(/[\][]/g, '').split(',').map(function(x) {
        return x ? x : 'null'
      }).join(',')
    );

  • answered 2018-02-13 03:17 clockwork

    var str = "[,,,1,2,,,3,4,,,,,,5,6]";
    var a = `[${str.slice(1, -1).split(",").map(n => n || "null")}]`
      
    console.log(a);

  • answered 2018-02-13 03:18 gavgrif

    As mentioned - the [ and ] are altering your return - this removes them and using the same function as your existing function - but on the shortened string and returns the correct value.

    var str = "[,,,1,2,,,3,4,,,,,,5,6]";
    let newStr = str.substring(1,str.length-1).split(',').map(function(x) { return x ? x : 'null' }).join(',')
    
    console.log(newStr);
    // Gives [null,null,null,1,2,null,null,3,4,null,null,null,null,null,5,6]
    
    
    var str = "[,,,1,2,,,3,4,,,,,,5,6,,]";
    
    newStr = str.substring(1,str.length-1).split(',').map(function(x) { return x ? x : 'null' }).join(',');
    console.log(newStr);
    // Gives null,null,null,1,2,null,null,3,4,null,null,null,null,null,5,6,null,null

  • answered 2018-02-13 03:49 Ari Singh

    String.prototype.repEmptyWithNull = 
    function() {return this
      .replace(/,,/g, ',null,')
      .replace(/,,/g, ',null,')
      .replace('[,', '[null,')
      .replace(',]', ',null]')
    }
    
    console.log ("[,,,1,2,,,3,4,,,,,,5,6,]".repEmptyWithNull())
    console.log ("[,,1,2,,,3,4,,,,,,5,6]".repEmptyWithNull())
    console.log ("[,1,2,,,3,4,,,,,,5,6,,]".repEmptyWithNull())
    console.log ("[,1,2,,,3,4,,,,,,5,6]".repEmptyWithNull())
    console.log ("[1,2,,,3,4,,,,,,5,6,]".repEmptyWithNull())
    console.log ("[1,2,,,3,4,,,,,,5,6]".repEmptyWithNull())