Home | Contact Us | FAQ | Search & Site Map | Link to Us
Sign In | Join | Other 45 Sites in Network
Home
Discussion GroupsGeneralPHPASPPerlColdFusionFlashHTML, CSS, ScriptsBrowsers

Webmaster Forum / HTML, CSS, Scripts / JavaScript / March 2005



Tip: Looking for answers? Try searching our database.

RegEx re Replace

Thread view: 
Enable EMail Alerts  Start New Thread
Thread rating: 
McKirahan - 31 Mar 2005 18:26 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).

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.
Ivo - 31 Mar 2005 18:38 GMT
> 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
RobB - 31 Mar 2005 18:45 GMT
> 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
Dr John Stockton - 31 Mar 2005 22:08 GMT
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
 
Sign In
Join
My Latest Posts
My Monitored Threads
My Blog
My Photo Gallery
My Profile
My Homepage

Start New Thread
Enable EMail Alerts
Rate this Thread



©2009 Advenet LLC   Privacy Policy - Terms of Use
This website includes both content owned or controlled by Advenet as well as content owned or controlled by third parties.