Hello and welcome to our community! Is this your first visit?
Register
Enjoy an ad free experience by logging in. Not a member yet? Register.
Results 1 to 6 of 6
  1. #1
    New Coder
    Join Date
    May 2013
    Posts
    24
    Thanks
    12
    Thanked 0 Times in 0 Posts

    Regex to get a number from start of email subject

    Hi,

    Regex help please!

    I have an email subject that contains a reference number at the beginning. I just need to grab that number, and then add to a database record.

    I'm just not sure of the syntax.

    This works on it's own
    Code:
    \w+[0-9]
    but how do I use it?

    Code:
    var strSubjectLine = new String(email.subject);
    var rxIncidentNum = strSubjectLine.replace(w+[0-9]);
    I received an error leaving the \ before the w

    but am I on the right track?

  • #2
    Senior Coder
    Join Date
    Dec 2010
    Posts
    2,355
    Thanks
    11
    Thanked 558 Times in 551 Posts
    You want to get the number from the beginning of a string? Try parseInt which will automatically do this
    Code:
    var myString = "125This is the rest of the string";
    var myNumber = parseInt(myString, 10);
    alert(myNumber);   // will give 125

  • #3
    Supreme Master coder! Philip M's Avatar
    Join Date
    Jun 2002
    Location
    London, England
    Posts
    17,731
    Thanks
    202
    Thanked 2,508 Times in 2,486 Posts
    If the (single) number is not right at the beginning of the string:-

    Code:
    <script type = "text/javascript">
    
    var myString = "This number 125 is followed by the rest of the string";
    var myString = myString.replace(/[^\d]/g,"");
    alert (myString);
    
    </script>
    Or perhaps

    <script type = "text/javascript">

    var myString = "This number 125 is followed by another number 456 and the rest of the string";
    myString2 = myString.replace(/^(\D+)(\d+).*$/,"$2")
    alert (myString2);

    </script>

    Looking after up to six kids will be hard. Staff have only two pairs of hands, after all. - Presenter BBC Radio 2
    Last edited by Philip M; 09-26-2013 at 07:43 AM.

    All the code given in this post has been tested and is intended to address the question asked.
    Unless stated otherwise it is not just a demonstration.

  • #4
    New Coder
    Join Date
    May 2013
    Posts
    24
    Thanks
    12
    Thanked 0 Times in 0 Posts
    Thanks for the reply, that sort of worked.

    It added a .0 to the end, treating it like a real number I suppose. When really, as a reference number it doesn't need to be like that.

    01679460 - New Assignment
    returned:
    1679460.0

    any ideas to fix that little anomaly?

  • #5
    Supreme Master coder! Philip M's Avatar
    Join Date
    Jun 2002
    Location
    London, England
    Posts
    17,731
    Thanks
    202
    Thanked 2,508 Times in 2,486 Posts
    01679460 with a leading zero is not a number - it is a string.


    Code:
    <script type = "text/javascript">
    
    var myString = "01679460 - New Assignment";
    myString2 = myString.replace(/(\D+)/g,"");
    alert (typeof myString2 + "  "  + myString2);
    
    </script>

    All the code given in this post has been tested and is intended to address the question asked.
    Unless stated otherwise it is not just a demonstration.

  • #6
    New Coder
    Join Date
    May 2013
    Posts
    24
    Thanks
    12
    Thanked 0 Times in 0 Posts
    Thank you! Worked great.


  •  

    Posting Permissions

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