-1

I have a problem with regex. I would like to make a test to see if a string contains a word char. If it contains a word char it should alert true. And for some reason this alerts true. What am i doing wrong? I'm pretty positive that this shouldn't alert true.

if (/\w/.test('23')) {alert('true')}
3
  • 2
    "2" and "3" are alphanumeric characters. Commented Jun 30, 2014 at 18:54
  • \w doesn't mean "word character;" it means "alphanumeric character," as @NobleMushtak pointed out.
    – elixenide
    Commented Jun 30, 2014 at 18:58
  • /\w[^0-9]/.test('9999') Commented Jun 30, 2014 at 18:58

1 Answer 1

7

/\w/ tests for alphanumeric characters (including the underscore). Instead, make your own regular expression that contains only alphabetic characters:

/[A-Za-z]/.test("hi"); //true
/[A-Za-z]/.test("HI!"); //true
/[A-Za-z]/.test("23"); //false

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.