SitePoint Sponsor

User Tag List

Results 1 to 5 of 5

Thread: regex help

  1. #1
    SitePoint Evangelist
    Join Date
    Apr 2005
    Location
    London, UK
    Posts
    506
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)

    regex help

    Folks,

    I want to allow a string hat either contains a space or not. e.g. "1234AB" or "1234 AB".

    my current regex only allows a string with a space, but not when it has no space.

    Code:
    var input   = "1234 AB";
    var string = input.match(/^[1-9]\d{3}\s[A-Z]{2}$/)[0];
    document.write(string);

  2. #2
    SitePoint Evangelist
    Join Date
    Apr 2005
    Location
    London, UK
    Posts
    506
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Quote Originally Posted by ma201dq View Post
    Folks,

    I want to allow a string hat either contains a space or not. e.g. "1234AB" or "1234 AB".

    my current regex only allows a string with a space, but not when it has no space.

    Code:
    var input   = "1234 AB";
    var string = input.match(/^[1-9]\d{3}\s[A-Z]{2}$/)[0];
    document.write(string);
    Actually I got this working by specifying a \s{0,1}.

  3. #3
    Unobtrusively zen silver trophybronze trophy
    paul_wilkins's Avatar
    Join Date
    Jan 2007
    Location
    Christchurch, New Zealand
    Posts
    14,709
    Mentioned
    102 Post(s)
    Tagged
    4 Thread(s)
    Quote Originally Posted by ma201dq View Post
    Actually I got this working by specifying a \s{0,1}.
    The trouble with using \s{0,1} is that it will be okay with a space or no space, but if a dash is used, such as in 1234-AB it will fail to work.

    Using [ \-] instead of \s is a more robust solution, if there might be any risk of a dash be used as a separator instead.
    Programming Group Advisor
    Reference: JavaScript, Quirksmode Validate: HTML Validation, JSLint
    Car is to Carpet as Java is to JavaScript

  4. #4
    Programming Since 1978 silver trophybronze trophy felgall's Avatar
    Join Date
    Sep 2005
    Location
    Sydney, NSW, Australia
    Posts
    16,826
    Mentioned
    25 Post(s)
    Tagged
    1 Thread(s)
    \s{0,1} is the same as \s?

    ? = {0,1}
    * = {0,}
    + = {1,}
    Stephen J Chapman

    javascriptexample.net, Book Reviews, follow me on Twitter
    HTML Help, CSS Help, JavaScript Help, PHP/mySQL Help, blog
    <input name="html5" type="text" required pattern="^$">

  5. #5
    Unobtrusively zen silver trophybronze trophy
    paul_wilkins's Avatar
    Join Date
    Jan 2007
    Location
    Christchurch, New Zealand
    Posts
    14,709
    Mentioned
    102 Post(s)
    Tagged
    4 Thread(s)
    Quote Originally Posted by felgall View Post
    \s{0,1} is the same as \s?
    No, \s{1} is the same as \s

    What I was saying earlier is that \s is less capable of picking up formatting variations that he mentioned in a related thread

    Given a string such as "1234-abc" or "1234 abc" or "1234abc"
    So for the "1234-abc" to be successful, you cannot use \s for that, and must instead use something else such as [ \-]

    Which means that what used to be \s{0,1} would now be [ \-]{0,1}
    which I would preferably use as [ \-]? where the question mark indicates that it's optional.
    Programming Group Advisor
    Reference: JavaScript, Quirksmode Validate: HTML Validation, JSLint
    Car is to Carpet as Java is to JavaScript


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
  •