I have a function to add commas to numbers:
function commafy( num ) {
num.toString().replace( /\\B(?=(?:\\d{3})+)$/g, \",\" );
}
Unfortun
Assuming your usage examples are not representative of already-working code but instead desired behavior, and you are looking for help with the algorithm, I think you are already on the right track with splitting on any decimals.
Once split, apply the existing regex to the left side, a similiar regex adding the spaces instead of commas to the right, and then rejoin the the two into a single string before returning.
Unless, of course, there are other considerations or I have misunderstood your question.