How to write the following regular express?

Discussion in 'Javascript' started by nickwang@gmail.com, Jul 17, 2007.

  1. Guest

    123ABC123 => 123ABC, 123
    343BBB => 343BBB, null
    12323 => null, 12322

    (?<first>.*\D+)(?<second>\d*) doesn't work on the third line.
     
    , Jul 17, 2007
    #1
    1. Advertising

  2. RobG Guest

    wrote:
    > 123ABC123 => 123ABC, 123
    > 343BBB => 343BBB, null
    > 12323 => null, 12322
    >
    > (?<first>.*\D+)(?<second>\d*) doesn't work on the third line.
    >


    Try:

    var data = {
    x : '123ABC456',
    y : '343BBB',
    z : '12345'
    };

    var p, t, u,
    re = /(^\d+[^\d]+|^.*?)(\d*$)/g;

    for (p in data) {
    u = [];
    while (t = re.exec(data[p])) {
    u[0] = t[1] || null;
    u[1] = t[2] || null;
    }
    alert('String: ' + data[p] + '\n\n' + u.join(' : '));
    }


    You could also use:

    re = /(^\d+[^\d]+|^)(\d*$)/g;


    or if ^\d doesn't suit (it will match any non-digit):

    re = /(^\d+[A-Z]+|^)(\d*$)/g;


    or

    re = /(^\d+[a-z]+|^)(\d*$)/ig;


    may be safer - take your pick. Tested in Safari, Firefox and Opera.



    --
    Rob
    "We shall not cease from exploration, and the end of all our
    exploring will be to arrive where we started and know the
    place for the first time." -- T. S. Eliot
     
    RobG, Jul 17, 2007
    #2
    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. ad
    Replies:
    7
    Views:
    701
    Scott Allen
    Apr 11, 2005
  2. =?Utf-8?B?Q2FybCBNLg==?=
    Replies:
    4
    Views:
    2,516
    sivlookingforu
    Aug 7, 2006
  3. AndyL
    Replies:
    6
    Views:
    277
    Felipe Almeida Lessa
    Mar 28, 2006
  4. brooksr
    Replies:
    3
    Views:
    346
    Jon Paal
    Dec 24, 2006
  5. Jake Henderson

    Visual Web Developer 2005 Express and SQL 2005 Express

    Jake Henderson, Mar 10, 2006, in forum: ASP .Net Web Services
    Replies:
    0
    Views:
    261
    Jake Henderson
    Mar 10, 2006
Loading...

Share This Page