Regexp not working

Hello,

The current code returns true when it shouldn’t… Can someone spot the issue?

:slight_smile:


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

I got false when I ran it. What browser/os are you using? Is there context that might be messing with the results somehow?

var foo = /[a-z]/.test(“aaa1”);
alert(foo);

returns true.

:slight_smile:

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:


// ^ means "start of string"
// * means "zero or more"
// $ means "end of string"
var foo = /^[a-z]*$/.test('aaa1');

alert(foo); // alerts "false"

:tup: