SitePoint Sponsor

User Tag List

Results 1 to 3 of 3

Thread: A question about regular expressions

  1. #1
    SitePoint Addict
    Join Date
    Nov 2009
    Posts
    249
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)

    A question about regular expressions

    Hi,

    I have a variable string and I would like to check whether it contains the following or not:

    "digit"+"digit"+":"

    For example: "02:", "45:", "13:", "88:" etc.

    I tried the following but it didn't work:

    HTML Code:
    myString.indexOf(/[0-9]{2}$/+':')
    Thanks for any ideas.

  2. #2
    Grüße aus'm Pott
    SitePoint Award Recipient Pullo's Avatar
    Join Date
    Jun 2007
    Location
    Germany
    Posts
    2,744
    Mentioned
    47 Post(s)
    Tagged
    3 Thread(s)
    Hi,

    \d will match any digit and the colon is just a normal character, so you could write:

    Code JavaScript:
    var str="This is my string 99:99"; 
    if (str.match(/\d\d:/)){
      alert("Match found");
    }

    HTH
    How well do you know your JavaScript from your jQuery?
    Check out SitePoint's latest JavaScript challenge


    My blog

  3. #3
    SitePoint Addict
    Join Date
    Nov 2009
    Posts
    249
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Quote Originally Posted by Pullo View Post
    Hi,

    \d will match any digit and the colon is just a normal character, so you could write:

    Code JavaScript:
    var str="This is my string 99:99"; 
    if (str.match(/\d\d:/)){
      alert("Match found");
    }

    HTH
    Thanks a lot, it worked.

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
  •