RegEx re Replace

M

McKirahan

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.
 
I

Ivo

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.

'1111222233334444'.replace( /(\w{4})/g, '$1 ' ).replace(/ +$/,'');

The first replacement puts the spaces in, the second one removes the
trailing space(s).
hth
 
R

RobB

McKirahan said:
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.

x = x.replace(/(\d{3}\d\B)/g, '$1 ');
 
M

McKirahan

RobB said:
McKirahan said:
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).
[snip]

x = x.replace(/(\d{3}\d\B)/g, '$1 ');

Excellent!

Any recommendations on learning RegEx?

I have O'Reilly's "Mastering Regular Expressions"
but I haven't spent the time with it yet.
 
R

RobB

McKirahan said:
RobB said:
McKirahan said:
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).
[snip]

x = x.replace(/(\d{3}\d\B)/g, '$1 ');

Excellent!

Any recommendations on learning RegEx?

I have O'Reilly's "Mastering Regular Expressions"
but I haven't spent the time with it yet.

That O'Reilly book is phenomenal (although notably missing *any*
JavaScript implementations). I blow at regexes - but, for what it's
worth, try these...

http://www.webreference.com/js/column5/
http://www.regular-expressions.com/
http://www.devarticles.com/c/a/JavaScript/Regular-expressions-in-JavaScript/
http://d0om.fnal.gov/d0admin/doctaur/dtdocs/p-langs/web_prog_bookshelf/jscript/ch10_01.htm
http://www.evolt.org/article/Regular_Expressions_in_JavaScript/17/36435/
Any recommendations on learning RegEx?

Get a big bottle of Advil first. #:=o
 
D

Dr John Stockton

JRS: In article <[email protected]>, dated Thu, 31 Mar
2005 11:26:13, seen in McKirahan
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.

Look for code that inserts thousands separators, change comma to space
and 3 to 4 - js-maths.htm .

That's if
T = S.replace(/^(\d{4})(\d{4})(\d{4})(\d{4})$/, "$1 $2 $3 $4")
does not please you.

CC numbers all have 16 digits, do they not?
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,764
Messages
2,569,566
Members
45,041
Latest member
RomeoFarnh

Latest Threads

Top