Search code examples
javascriptregexsplitalphadigits

split digits and alpha


I need to split a string like this: ID00605button. I want to get out ID00605. my code example:

    var contentLeftID;
    var id = "ID00605button"
    id = id.split(*some regex*);

    contentLeftID = id[0] + id[1];

Is it somebody how knows to write a regex to get ID00605?


Solution

  • This should work:

    var contentLeftID;
    var id = "ID00605button";
    id = id.match(/(ID\d+)(.*)/);
    contentLeftID = id[1] + id[2];
    

    id.match(/(ID\d+)(.*)/) returns an array:

    [ "ID00605button", "ID00605", "button" ]