RegEx re Replace

Discussion in 'Javascript' started by McKirahan, Mar 31, 2005.

  1. McKirahan

    McKirahan Guest

    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.
     
    McKirahan, Mar 31, 2005
    #1
    1. Advertising

  2. McKirahan

    Ivo Guest

    "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
     
    Ivo, Mar 31, 2005
    #2
    1. Advertising

  3. McKirahan

    RobB Guest

    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 ');
     
    RobB, Mar 31, 2005
    #3
  4. McKirahan

    McKirahan Guest

    "RobB" <> wrote in message
    news:...
    > 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.
     
    McKirahan, Mar 31, 2005
    #4
  5. McKirahan

    RobB Guest

    McKirahan wrote:
    > "RobB" <> wrote in message
    > news:...
    > > 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/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
     
    RobB, Mar 31, 2005
    #5
  6. JRS: In article <>, dated Thu, 31 Mar
    2005 11:26:13, seen in news:comp.lang.javascript, McKirahan
    <> 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.
     
    Dr John Stockton, Mar 31, 2005
    #6
  7. McKirahan

    Csaba Gabor Guest

    McKirahan wrote:
    > "RobB" <> wrote in message
    > news:...

    ....
    >>>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
     
    Csaba Gabor, Mar 31, 2005
    #7
  8. McKirahan

    McKirahan Guest

    McKirahan, Apr 1, 2005
    #8
    1. Advertising

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

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Brian Blais
    Replies:
    1
    Views:
    394
    Bruno Desthuilliers
    Jun 27, 2006
  2. Greg Ewing
    Replies:
    2
    Views:
    357
    Dieter Maurer
    Jun 29, 2006
  3. Alun
    Replies:
    3
    Views:
    4,575
    Masudur
    Feb 18, 2008
  4. Replies:
    3
    Views:
    794
    Reedick, Andrew
    Jul 1, 2008
  5. Prasad S
    Replies:
    2
    Views:
    245
    Dr John Stockton
    Aug 27, 2004
Loading...

Share This Page