var category = "a()";
if(category.charAt(0) == /^[a-zA-Z]+$/){
/*This part doesn't gets executed*/
/*What is the problem with the if condition?*/
}
答案 0 :(得分:9)
您正在将您的角色与正则表达式的实例进行比较。 你实际上想用正则表达式测试你的角色。
你可以这样做:
var category = "a()";
if (/^[a-zA-Z]+$/.test(category.charAt(0))) {
// Now it will get executed
}
进一步阅读JavaScript正则表达式风格:
http://www.regular-expressions.info/javascript.html