SitePoint Sponsor

User Tag List

Results 1 to 5 of 5
  1. #1
    SitePoint Zealot
    Join Date
    Nov 2007
    Posts
    120
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)

    matching all integers

    Hey,

    I'm not the best at regex so I came here for some help!

    I'm trying to match all integers before and after the hyphen:

    12345-5

    This is what I tried but always returns null

    Code:
    var divID = '12345-5';
    var idPattern = /^[0-9]+$/; //Matching one or more numbers before the hyphen
    var id2Pattern = /^\-[0-9]+$/; //Matching starts from the hyphen and all numbers that proceed
    var id2 = divID.match(id2Pattern);
    
    alert(id2);
    Thanks

  2. #2
    SitePoint Guru
    Join Date
    Apr 2006
    Posts
    802
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    var divID = '12345-5';
    alert(divID.replace(/[^\d\-]+/g,'').split('-'))

    Or even divId.split('-'), if you are sure of the input format

  3. #3
    Unobtrusively zen silver trophybronze trophy
    paul_wilkins's Avatar
    Join Date
    Jan 2007
    Location
    Christchurch, New Zealand
    Posts
    14,702
    Mentioned
    101 Post(s)
    Tagged
    4 Thread(s)
    You have another problem too. It's invalid for an identifier to start with a number.

    Other than that, you can use \d instead of [0-9], and by matching /\d+/ you'll get an array of all groups of numbers. ['12345', '5']

    Code javascript:
    var divID = '12345-5';
    var matches = divID.match(/\d+/);
    var id2 = matches[1];
    alert(id2); // alerts 5
    Programming Group Advisor
    Reference: JavaScript, Quirksmode Validate: HTML Validation, JSLint
    Car is to Carpet as Java is to JavaScript

  4. #4
    SitePoint Zealot
    Join Date
    Nov 2007
    Posts
    120
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Hi,

    I've had a chance to test out pmw57 way and it's not returning an array.

    If I do matches[1] it returns undefined but if I do matches[0] it returns all numbers.

    Another thing to note, all numbers before the hyphen will be unique. So it can be 12-4 or 14587-1 etc.

    EDIT:

    Went with mrhoo way. Worked perfect.

    thanks.

  5. #5
    Unobtrusively zen silver trophybronze trophy
    paul_wilkins's Avatar
    Join Date
    Jan 2007
    Location
    Christchurch, New Zealand
    Posts
    14,702
    Mentioned
    101 Post(s)
    Tagged
    4 Thread(s)
    Quote Originally Posted by YBH305 View Post
    Hi,

    I've had a chance to test out pmw57 way and it's not returning an array.
    Sorry man, the global setting was missing when it was typed out.

    It should be /\d+/g

    Code javascript:
    var divID = '12345-5';
    var matches = divID.match(/\d+/g);
    var id2 = matches[1];
    alert(id2); // alerts 5
    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
  •