> 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
> Can someone help me out?
>
[quoted text clipped - 22 lines]
>
> Thanks in advance.
x = x.replace(/(\d{3}\d\B)/g, '$1 ');
McKirahan - 31 Mar 2005 19:14 GMT
> > 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.
RobB - 31 Mar 2005 19:26 GMT
> > > Can someone help me out?
> > >
[quoted text clipped - 11 lines]
> 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/c
h10_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
Csaba Gabor - 31 Mar 2005 23:18 GMT
...
>>>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.pcre.pattern.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
JRS: In article <NsidnUA7kvQCr9HfRVn-1g@comcast.com>, dated Thu, 31 Mar
2005 11:26:13, seen in news:comp.lang.javascript, McKirahan
<News@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?

Signature
© 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.
McKirahan - 01 Apr 2005 00:09 GMT
[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/val-credit-card.html