Can someone help me out?
I'm looking for a regular expression that inserts a space
into a string after every fourth byte (except the last).
Specifically, this is to display credit card numbers.
alert(doit("4444555566667777"));
// should return "4444 5555 6666 7777"
function doit(ccno) {
if (ccno.length % 4 != 0) return;
var what = ??????
return what;
}
Here's a non-regular expression approach:
var what = "";
for (var i=1; i<ccno.length/4+1; i++) {
if (what != "") what += " ";
what += ccno.substring(i*4-4,i*4);
}
Thanks in advance.