0

Hi!

I am looking for a function that will add spaces in a string in a certain manner:

tmpStr: 123456789123 (always 12 digits)
the desired outcome is: 123 456 78 91 23

Do anyone of you have a neat fuction for that? :) I found one question in this forum (http://www.daniweb.com/forums/thread253055.html), but since I do have a different interval for my spaces I have not been able to make it work. Thanks in advance!

2
Contributors
3
Replies
4
Views
6 Years
Discussion Span
Last Post by kalleanka
0

If your interval and string length is fixed then you could use:

tmpStr.replace(/(\d{3})(\d{3})(\d{2})(\d{2})(\d{2})/g, '$1 $2 $3 $4 $5');

Edited by pritaeas: n/a

0

Thaaaaanks for such quick answer!!! :D I tried your code, but I did not get it to add the space in the string - may I have forgotten something else...?

var tmp = document.forms[0].testField.value;
alert(tmp.replace(/(\d{3})(\d{3})(\d{2})(\d{2})(\d{2})/g, '$1 $2 $3 $4 $5'));

0

Hmmm, when trying to add them one by one it now works like charm!! :) Thanks so much, you have saved me so much time!!!

This question has already been answered. Start a new discussion instead.
Have something to contribute to this discussion? Please be thoughtful, detailed and courteous, and be sure to adhere to our posting rules.