SitePoint Sponsor

User Tag List

Results 1 to 4 of 4
  1. #1
    SitePoint Evangelist
    Join Date
    Apr 2005
    Location
    London, UK
    Posts
    506
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)

    javascript regex help

    Folks;

    I need some help on my regex. Given a string such as "1234-abc" or "1234 abc" or "1234abc", I simply want to use a regex that splits the string into two parts. i.e. "1234" and "abc". So the output would be an array like so: array[0] = "1234" and array[1] = "abc".

    Here is my current code which works fine for the number part, but does not handle the text part correctly.

    PHP Code:
     <html>

    <
    head>

    <
    script type="text/javascript">

    function 
    doit(){
    var 
    input   "1234-abc";
    var 
    numberinput.match(/\d+/)[0];      //this currently prints 1234
    var text input.match(/[\w]{0,200}\d/)[0];  //but this prints abc1234

    document.write(number);
    document.write(text);

    }

    doit();

    </script>
    </head>
    <body>

    </body>
    </head> 

  2. #2
    SitePoint Evangelist
    Join Date
    Apr 2005
    Location
    London, UK
    Posts
    506
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Quote Originally Posted by ma201dq View Post
    Folks;

    I need some help on my regex. Given a string such as "1234-abc" or "1234 abc" or "1234abc", I simply want to use a regex that splits the string into two parts. i.e. "1234" and "abc". So the output would be an array like so: array[0] = "1234" and array[1] = "abc".

    Here is my current code which works fine for the number part, but does not handle the text part correctly.

    PHP Code:
     <html>

    <
    head>

    <
    script type="text/javascript">

    function 
    doit(){
    var 
    input   "1234-abc";
    var 
    numberinput.match(/\d+/)[0];      //this currently prints 1234
    var text input.match(/[\w]{0,200}\d/)[0];  //but this prints abc1234

    document.write(number);
    document.write(text);

    }

    doit();

    </script>
    </head>
    <body>

    </body>
    </head> 
    Actually I got it working now like so:

    PHP Code:
    var numberinput.match(/\d+/)[0];      
    var 
    text input.match(/[a-zA-Z]+$/)[0]; 

  3. #3
    Hosting Team Leader silver trophybronze trophy
    cpradio's Avatar
    Join Date
    Jun 2002
    Location
    Ohio
    Posts
    5,243
    Mentioned
    155 Post(s)
    Tagged
    0 Thread(s)
    Code:
    function doit(){ 
    var input   = "1234-abc"; 
    var text = input.match(/(\d+)|(\w+)/g);  //but this prints abc1234 
    document.write(text[0]); 
    document.write(text[1]); 
    
    }

  4. #4
    Unobtrusively zen silver trophybronze trophy
    paul_wilkins's Avatar
    Join Date
    Jan 2007
    Location
    Christchurch, New Zealand
    Posts
    14,729
    Mentioned
    104 Post(s)
    Tagged
    4 Thread(s)
    Quote Originally Posted by ma201dq View Post
    Actually I got it working now like so:

    PHP Code:
    var numberinput.match(/\d+/)[0];      
    var 
    text input.match(/[a-zA-Z]+$/)[0]; 
    The most efficient way to do it is by using two capture groups within the regular expression.

    What you want is to capture numbers at the start (\d+)
    and for there to be an optional space or a dash [ \-]?
    and to then capture some word characters (\w+)

    Which ends up being:

    Code javascript:
    var match = input.match((/\d+)[ \-]?(\w+)/),
        // match[0] is the complete string that fits with the regular expression
        // match[1] is the first capture group, from (\d+)
        number = match[1],
        // match[2] is the second capture group, from (\w+)
        text = match[2];
    Programming Group Advisor
    Reference: JavaScript, Quirksmode Validate: HTML Validation, JSLint
    Car is to Carpet as Java is to JavaScript


Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •