SitePoint Sponsor

User Tag List

Results 1 to 4 of 4
  1. #1
    SitePoint Enthusiast
    Join Date
    Sep 2005
    Posts
    87
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)

    Split numbers from string

    Hello,

    I have this string:
    field = "due_date31";

    is there a way to split it into two vars so it will be like:
    var 1 = "due_date";
    var 2 = "31";

    any ideas?

    thanks!

  2. #2
    SitePoint Wizard Stomme poes's Avatar
    Join Date
    Aug 2007
    Location
    Netherlands
    Posts
    10,278
    Mentioned
    50 Post(s)
    Tagged
    2 Thread(s)
    Is "field" always "due_date##"?

    If so you could use .substring(8, 11) to get the number for your second variable (though it would still be a string "31" and not really a number until you make it one).

    For the first one you could maybe match a pattern?
    field="due_date31";
    var textchars=/([\w_]+)/; //assuming you only ever have letters and underscores
    var a=field.replace(textchars, $1);

    Maybe easier to use matching regexes for both?
    Code:
    field="due_date31";
    var pattern=/([\w_]+)(\d+)/;
    var a=field.replace(pattern, $1);
    var b=field.replace(pattern, $2);
    I dunno what all you have, and I expect my solutions to be clunky at best : ) My pattern is assuming field is always letters/underscores followed by numbers.

  3. #3
    SitePoint Enthusiast
    Join Date
    Sep 2005
    Posts
    87
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    no, it is not due_date always. It depends on the field that sending the request

  4. #4
    SitePoint Wizard Stomme poes's Avatar
    Join Date
    Aug 2007
    Location
    Netherlands
    Posts
    10,278
    Mentioned
    50 Post(s)
    Tagged
    2 Thread(s)
    no, it is not due_date always. It depends on the field that sending the request
    If using a pattern-match is the best method (and I don't know that it is) then the pattern var would need to be changed to fit your data set. I'd also make sure the numbers you are getting are really numbers and not "31" strings, if it matters.


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
  •