Split string every 3 characters using JavaScript

How do we split the string every 3 characters from the back using JavaScript?

Let's say I have this:

str = 9139328238

      

after the desired function, it becomes:

parts = ['9','139','328','238']

      

How do we do it elegantly?

+3


source to share


5 answers


var myString = String( 9139328238 );
console.log( myString.split( /(?=(?:...)*$)/ ) );
// ["9", "139", "328", "238"]

      

I cannot guarantee any performance guarantees. For small lines, this should be fine.



Here's the loop implementation:

function funkyStringSplit( s )
{
    var i = s.length % 3;
    var parts = i ? [ s.substr( 0, i ) ] : [];
    for( ; i < s.length ; i += 3 )
    {
        parts.push( s.substr( i, 3 ) );
    }
    return parts;
}

      

+11


source


Not so elegant, but will show you a while loop



 function commaSeparateNumber (val) {
    val = val.toString();
    while (/(\d+)(\d{3})/.test(val)){
      val = val.replace(/(\d+)(\d{3})/, '$1'+','+'$2');
    }
    return val;
  }

  var str = "9139328238";
  var splitStr = commaSeparateNumber(str).split(",");
  console.log(splitStr);

      

0


source


Try the following:

var str = 9139328238 + ''; //convert int to string
var reqArr = []; // required array
var len = str.length; //maintaining length
while (len > 0) {
    len -= 3;
    reqArr.unshift(str.slice(len)); //inserting value to required array
    str = str.slice(0, len); //updating string
}

      

Hope it helps.

0


source


Since regex operations don't like all different reasons : here is a regex function that uses a regex to split any regex string for every X characters from the back. Nothing fancy, but it works:

function splitStringFromEnd(customString, every) {
  var result = [], counter = every;
  // loop that captures substring chungs of "every" length e.g.: 1000.00 -> ["000", ".00"]
  for (var i = counter; counter <= customString.length; counter += every) {
    result.unshift(customString.substr(customString.length - counter, every))
  }
  // check if there is a remainder and grabs it.
  // Using our 1000.00 example: diff = 9 - 7; remainder = 3 - 2; -> ["1", "000", ".00"]
  var diff = counter - customString.length;
  var remainder = every - diff;
  if(remainder > 0) { result.unshift(customString.substr(0, remainder)) }
  return result;
}

      

for your example it would be:

splitStringFromEnd("9139328238", 3);
// :returns => ["9", "139", "328", "238"]

      

Enjoy :)

0


source


Finally, it sounds good. This is what I got so far without using any loops

function breakAt3(x)
    {
            if(x.length < 3){ var parts = [x]; return parts; }
        var startPos = (x.length % 3);
        var newStr = x.substr(startPos); 
        var remainingStr = x.substr(0,startPos); 

        var parts = newStr.match(/.{1,3}/g);
        if(remainingStr != ''){ var length = parts.unshift(remainingStr); }
        return parts;

    }

    var str = '92183213081';  
    var result = breakAt3(str);  // 92,183,213,081 

      

-2


source







All Articles