How can I insert a character after every n characters in javascript?
Keep it simple
var str = "123456789";
var parts = str.match(/.{1,3}/g);
var new_value = parts.join("-"); //returns 123-456-789
With regex
"The quick brown fox jumps over the lazy dogs.".replace(/(.{5})/g,"$1$")
The q$uick $brown$ fox $jumps$ over$ the $lazy $dogs.$
cheers,
function chunk(str, n) {
var ret = [];
var i;
var len;
for(i = 0, len = str.length; i < len; i += n) {
ret.push(str.substr(i, n))
}
return ret
};
chunk("The quick brown fox jumps over the lazy dogs.", 5).join('$');
// "The q$uick $brown$ fox $jumps$ over$ the $lazy $dogs."