SitePoint Sponsor

User Tag List

Results 1 to 5 of 5
  1. #1
    SitePoint Member
    Join Date
    Oct 2010
    Posts
    3
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)

    Simple regex question

    Hi all, I have the following regular expression /[a-zA-Z0-9\-]/g

    Basically, I am trying to make a pattern such that a string has ONLY alphabetical characters and/or a hyphen. The above regex matches strings if it has at least one character in the range, so it doesn't really do what I want it to do. So let's say, for example, I try to match the string "Test-String:Yay" with the above regex, it will match, but the regex that I'm trying to get shouldn't match it because it has a colon, which is not among the permitted characters. Thank you for your time.

  2. #2
    SitePoint Wizard bronze trophy
    Join Date
    Jul 2006
    Location
    Augusta, Georgia, United States
    Posts
    4,147
    Mentioned
    16 Post(s)
    Tagged
    3 Thread(s)
    /[a-Za-z\-]*?/g

  3. #3
    SitePoint Member
    Join Date
    Oct 2010
    Posts
    3
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Quote Originally Posted by oddz View Post
    /[a-Za-z\-]*?/g
    That didn't quite work. From what I understand about regex (which isn't very much) is that the expression you provided looks to find at least one of the characters in the list (I'm not too sure about this). If I'm wrong, can you provide some explanation about the expression? Also, I'm trying to find an expression such that it doesn't match with any string that contains characters not specified in the list (so in my case, only alphabetical, numerical, and hyphens are allowed).

  4. #4
    Utopia, Inc. silver trophy
    ScallioXTX's Avatar
    Join Date
    Aug 2008
    Location
    The Netherlands
    Posts
    9,067
    Mentioned
    153 Post(s)
    Tagged
    2 Thread(s)
    Let me break this down
    Quote Originally Posted by Kiryu0111 View Post
    Basically, I am trying to make a pattern such that a string has ONLY
    That ONLY right there indicates that the can be no other characters before what you want to match, an no characters after what you want to match. So you need ^ to indicate the start of the string and $ to indicate the end of the string.

    Quote Originally Posted by Kiryu0111 View Post
    alphabetical characters and/or a hyphen.
    That would be ([a-zA-Z0-9-]+)
    Note that a dash doesn't have to be escaped when you use it in a character class!

    So the final regex is ^([a-zA-Z0-9-]+)$

    Rémon - Hosting Advisor

    SitePoint forums will switch to Discourse soon! Make sure you're ready for it!

    Minimal Bookmarks Tree
    My Google Chrome extension: browsing bookmarks made easy

  5. #5
    Utopia, Inc. silver trophy
    ScallioXTX's Avatar
    Join Date
    Aug 2008
    Location
    The Netherlands
    Posts
    9,067
    Mentioned
    153 Post(s)
    Tagged
    2 Thread(s)
    Wait, you don't want to match digits.
    Then it is

    ^([a-zA-Z-]+)$
    Rémon - Hosting Advisor

    SitePoint forums will switch to Discourse soon! Make sure you're ready for it!

    Minimal Bookmarks Tree
    My Google Chrome extension: browsing bookmarks made easy


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
  •