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!

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');

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'));

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

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.