1

i want to know is there any inbuild function to add spaces in javascript after particular interval..!!?

there is string '00009999' i want to add blank space 0000 9999 after every 4 digits.?

4
Contributors
5
Replies
15
Views
7 Years
Discussion Span
Last Post by Bernd_1
2
// add spaces after every 4 digits (make sure there's no trailing whitespace)
somestring.replace(/(\d{4})/g, '$1 ').replace(/(^\s+|\s+$)/,'')
1
// add spaces after every 4 digits (make sure there's no trailing whitespace)
somestring.replace(/(\d{4})/g, '$1 ').replace(/(^\s+|\s+$)/,'')

this script works only when the string is numeric not when it is aplhanumeric

anyways thanks for help

1

ohh i got it instead of d i have write 'w' to make it alphanumeric..!!

thanks buddy..!

can u explain this part..??

replace(/(^\s+|\s+$)/,'')

i will appreciate that..

0

That will remove any preceding or trailing white space from the string.

Also, you may want to phrase your initial question better :P You asked for a script to add a space 'after every 4 digits' which is what you got..
\w will match a-z, A-Z, 0-9 and underscores, so yes, replacing the d with a w should work if you want a space inserted after every 4 characters.

0

Thanks for the solution.

replace(/(^\s+|\s+$)/,'') seems not to take away preceding AND trailing white space at the same time.

When I tested it with Servoy/JavaScript, it took out only the leading white spaces and left the trailing inside.

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.