我收到警告,告诉我地址只需要包含字母数字字符..
我对正则表达式的看法是/^\w+$/
所以我放123 Lane Street
if (address == ""){
errors += "please enter a address \n";
} else {
addressRE = /^\w+$/;
if (address.match(addressRE)){
//console.log("address match");
//do nothing.
} else {
//console.log("address not a match");
errors += "Address should only contain alphanumeric characters \n";
} // end if
}
答案 0 :(得分:2)
123 Lane
中的空格字符不被视为字母数字。
您需要/^[a-z0-9 ]+$/i
i
打开不区分大小写的匹配。
在JS中:
if (/^[a-z0-9 ]+$/i.test(yourString)) {
// It matches!
} else {
// Nah, no match...
}