Reputation: 2233
How do we split a string every 3 characters from the back using JavaScript?
Say, I have this:
str = 9139328238
after the desired function, it would become:
parts = ['9','139','328','238']
How do we do this elegantly?
Upvotes: 11
Views: 17208
Reputation: 1
"12345678".split('').reverse().reduce((a, s) => (a[0].length<3?a[0]=s+a[0]:a.unshift(s),a), ['']);
Upvotes: 0
Reputation: 1
const price_format = (price) => {
let result = [];
let new_str = [...price].reverse().join("");
let rightSplit = new_str.match(/.{1,3}/g).reverse();
for (let item of rightSplit) {
result.push([...item].reverse().join(""));
}
return result.join(",");
}
let price = "2560000000";
console.log(price_format(price));
// output : 2,560,000,000
Upvotes: 0
Reputation: 4113
There are a lot of complicated answers here.
function group(value) {
return value.match(/\d{1,3}(?=(\d{3})*$)/g);
}
console.log(group('1'));
console.log(group('123'));
console.log(group('1234'));
console.log(group('12345'));
console.log(group('123456'));
console.log(group('1234567'));
console.log(group('12345678'));
console.log(group('123456789'));
Upvotes: 4
Reputation: 2321
I know this is an old question, but I would like to provide my own one-line version to solve the problem :)
"12345678".split('').reverse().join('').match(/.{1,3}/g).map(function(x){
return x.split('').reverse().join('')
}).reverse()
This basically reverses the string, captures the groups of 3 elements, reverses each group and then reverses the whole string.
The steps are:
"12345678" -> [1, 2, 3, 4, 5, 6, 7, 8] //.split('')
[1, 2, 3, 4, 5, 6, 7, 8] -> [8, 7, 6, 5, 4, 3, 2, 1] //.reverse()
[8, 7, 6, 5, 4, 3, 2, 1] -> "87654321" //.join('')
"87654321" -> [876, 543, 21] //.match(...)
[876, 543, 21] -> [678, 345, 12] //.map(function(x){...})
[678, 345, 12] -> [12, 345, 678] //.reverse()
You can then join the array with a character (e.g. the ',' for thousands separator)
[12, 345, 678].join(',') -> "12,345,678"
Upvotes: 4
Reputation: 4098
Since regex operations are not liked by everyone for various reasons: here is a regular function using a regular loop to split any regular string every X characters from 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 :)
Upvotes: 0
Reputation: 2233
Finally it seems good. This is what I have got till now 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
Upvotes: -2
Reputation: 3823
Try this:
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..
Upvotes: 0
Reputation: 1087
var myString = String( 9139328238 );
console.log( myString.split( /(?=(?:...)*$)/ ) );
// ["9", "139", "328", "238"]
I can't make any performance guarantees. For smallish strings it should be fine.
Here's a 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;
}
Upvotes: 21
Reputation: 207511
Not as elegant, but shows 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);
Upvotes: 0