Regular expression for checking if string is a-zA-Z0-9

javascript

Solution

`exec()` returns `null` if no match is found, which is `typeof` object not `undefined`.

You should use this:

var matches = pattern.exec(myString); // either an array or null
var matchStatus = Boolean(matches);

if (matchStatus)
    alert("there was a match");
else
    alert('within here');

Or just use the `test` method:

var matchStatus = pattern.test(myString); // a boolean

Problem

I am trying to check if a string is all `a-zA-Z0-9` but this is not working. Any idea why? ``` var pattern=/^[a-zA-Z0-9]*$/; var myString='125 jXw'; // this shouldn't be accepted var matches=pattern.exec(myString); var matchStatus=1; // say matchStatus is true if(typeof matches === 'undefined'){ alert('within here'); matchStatus=0; // matchStatus is false }; if(matchStatus===1){ alert("there was a match"); } ```

Original source