Monday, 15 July 2013

Split a string every 3 characters from back using JavaScript -



Split a string every 3 characters from back using JavaScript -

how split string every 3 characters using javascript.

str = 9139328238

and after desired function become

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

how elegantly?

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

i can't create performance guarantees. smallish strings should fine.

here's loop implementation:

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

javascript string split character

No comments:

Post a Comment