Javascript .charAt(0)给出了意想不到的结果

Aas*_*hil 1 javascript

var category = "a()";

if(category.charAt(0) == /^[a-zA-Z]+$/){


    /*This part doesn't gets executed*/
    /*What is the problem with the if condition?*/


}
Run Code Online (Sandbox Code Playgroud)

Wil*_*eer 9

您正在将您的角色与正则表达式的实例进行比较.您实际上想要使用正则表达式测试您的角色.

你可以这样做:

var category = "a()";

if (/^[a-zA-Z]+$/.test(category.charAt(0))) {
    // Now it will get executed
}
Run Code Online (Sandbox Code Playgroud)

关于JavaScript正则表达式的进一步阅读:http:
//www.regular-expressions.info/javascript.html