【发布时间】:2013-12-19 18:26:07
【问题描述】:
我必须检查地址字段中的数据输入。客户不希望用户使用 Rd 之类的术语。或 Rd 表示道路、大道或大道。对于大道等。我对大多数条款都没有问题。我有问题的地方是'Ave'让我们说。如果我寻找“AVE”,那很好,但它不会在字符串末尾的“AVE”上找到,如果我寻找“AVE”,它会在“Avenue”上得到误报,因为它会找到“Ave” ' 在那个字符串中。有人知道我该怎么做吗?
感谢您的帮助。 诺斯特
虽然 Q: 不是特定于语言的,但这是我在 JS 中的处理方式:
//function to check address for rd rd. ave ave. st st. etc
function checkaddy() {
//array of items to look for
var watchfor = Array();
watchfor[0] = " RD";
watchfor[1] = " RD.";
watchfor[2] = " AVE ";
watchfor[3] = " AVE.";
watchfor[4] = " ST ";
watchfor[5] = " ST.";
watchfor[6] = " BLVD.";
watchfor[7] = " CRT ";
watchfor[8] = " CRT.";
watchfor[9] = " CRES ";
watchfor[10] = " CRES.";
watchfor[11] = " E ";
watchfor[12] = " E.";
watchfor[13] = " W ";
watchfor[14] = " W.";
watchfor[15] = " N ";
watchfor[16] = " N.";
watchfor[17] = " S ";
watchfor[18] = " S.";
watchfor[19] = " PKWY ";
watchfor[20] = " PKWY.";
watchfor[21] = " DR ";
watchfor[22] = " DR.";
//get what the user has in the address box
var addcheck = $("#address").val();
//upper case the address to check
addcheck = addcheck.toUpperCase();
//check to see if any of these terms are in our address
watchfor.forEach(function(i) {
if (addcheck.search(watchfor[i]) > 0 ) {
alert("Found One!");
}
});
}
【问题讨论】:
-
尝试更好地直观地表达您的意思。我并没有真正关注
-
听起来你需要正则表达式。 Javascript 我猜? (w3schools.com/jsref/jsref_obj_regexp.asp)