By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
455,623 Members | 1,843 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 455,623 IT Pros & Developers. It's quick & easy.

RegEx re Replace

P: n/a
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.
Jul 23 '05 #1
Share this Question
Share on Google+
7 Replies


P: n/a
Ivo
"McKirahan" wrote

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
--
Ivo
Jul 23 '05 #2

P: n/a
McKirahan wrote:
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 ');

Jul 23 '05 #3

P: n/a
"RobB" <fe******@hotmail.com> wrote in message
news:11**********************@z14g2000cwz.googlegr oups.com...
McKirahan wrote:
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.
Jul 23 '05 #4

P: n/a
McKirahan wrote:
"RobB" <fe******@hotmail.com> wrote in message
news:11**********************@z14g2000cwz.googlegr oups.com...
McKirahan wrote:
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/JavaS...in-JavaScript/
http://d0om.fnal.gov/d0admin/doctaur...pt/ch10_01.htm
http://www.evolt.org/article/Regular...ript/17/36435/
Any recommendations on learning RegEx?


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

Jul 23 '05 #5

P: n/a
McKirahan wrote:
"RobB" <fe******@hotmail.com> wrote in message
news:11**********************@z14g2000cwz.googlegr oups.com... ....
I'm looking for a regular expression that inserts a space
into a string after every fourth byte (except the last).
....x = x.replace(/(\d{3}\d\B)/g, '$1 ');

.... Any recommendations on learning RegEx?

....

I learned from
http://php.net/manual/en/reference.p...ern.syntax.php
Dense, with many goodies javascript doesn't have,
but with many good examples, too, especially in the second half.

Csaba Gabor from Vienna
Jul 23 '05 #6

P: n/a
JRS: In article <Ns********************@comcast.com>, dated Thu, 31 Mar
2005 11:26:13, seen in news:comp.lang.javascript, McKirahan
<Ne**@McKirahan.com> posted :
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?

--
John Stockton, Surrey, UK. ?@merlyn.demon.co.uk Turnpike v4.00 IE 4
<URL:http://www.jibbering.com/faq/> JL/RC: FAQ of news:comp.lang.javascript
<URL:http://www.merlyn.demon.co.uk/js-index.htm> jscr maths, dates, sources.
<URL:http://www.merlyn.demon.co.uk/> TP/BP/Delphi/jscr/&c, FAQ items, links.
Jul 23 '05 #7

P: n/a
"Dr John Stockton" <sp**@merlyn.demon.co.uk> wrote in message
news:pf**************@merlyn.demon.co.uk...

[snip]
CC numbers all have 16 digits, do they not?

--
John Stockton, Surrey, UK. ?@merlyn.demon.co.uk Turnpike v4.00 IE

4
I think American Express and Diners Club are 15 digits.

http://money.howstuffworks.com/credit-card2.htm
http://javascript.internet.com/forms...edit-card.html
Jul 23 '05 #8

This discussion thread is closed

Replies have been disabled for this discussion.