SitePoint Sponsor

User Tag List

Results 1 to 5 of 5

Hybrid View

  1. #1
    SitePoint Zealot
    Join Date
    Jul 2011
    Posts
    199
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)

    regexp not working

    Hello,

    The current code returns true when it shouldn't... Can someone spot the issue?



    Code:
    var foo = /[a-z]/.test("12345");
    alert(foo);

  2. #2
    SitePoint Addict sdleihssirhc's Avatar
    Join Date
    Feb 2009
    Posts
    387
    Mentioned
    1 Post(s)
    Tagged
    0 Thread(s)
    I got false when I ran it. What browser/os are you using? Is there context that might be messing with the results somehow?
    I'm the web overlord for Graphic Business Systems

  3. #3
    SitePoint Zealot
    Join Date
    Jul 2011
    Posts
    199
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    var foo = /[a-z]/.test("aaa1");
    alert(foo);

    returns true.


  4. #4
    SitePoint Addict sdleihssirhc's Avatar
    Join Date
    Feb 2009
    Posts
    387
    Mentioned
    1 Post(s)
    Tagged
    0 Thread(s)
    Ah, yes, that would be because your regex is only looking for a single character. It will return true if it finds any lowercase letter.

    If you want to search the entire string... Well, there are a bunch of different ways you could do it; regular expressions are complicated. And I don't have a lot of experience with them, but this is what I would use, just off the top of my head:

    Code JavaScript:
    // ^ means "start of string"
    // * means "zero or more"
    // $ means "end of string"
    var foo = /^[a-z]*$/.test('aaa1');
     
    alert(foo); // alerts "false"
    I'm the web overlord for Graphic Business Systems

  5. #5
    SitePoint Zealot
    Join Date
    Jul 2011
    Posts
    199
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)


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
  •