0
我試圖寫一個搜索功能,允許用戶輸入以下標準之一返回'胡志明市'。對我來說棘手的事情是,它必須始終搜索任何詞的開始。除此之外,它還必須與其他語言一起工作,但是如果我能得到拉丁字符,那至少是一個開始。javascript詞搜索帶有或沒有空格從詞的開頭
//ho chi minh city
//hochiminhcity
//chi minh
//chiminh
//chiminhcity
//chi minh city
//ho
//chi
//minh
//city
containsTerm: function containsTerm (needle, haystack) {
var _this = LocationPickerCtrl,
ndl = _this.formatStr(stringHelper.escapeRegExp(needle)),
re = new RegExp('(?:^|\\s)' + ndl, 'gi'),
found = false,
len,
key = _this.formatStr(haystack.Key),
name = _this.formatStr(haystack.Name);
//check airport code
if (key.indexOf(ndl) > -1) {
found = true;
}
//start of word
if (re.test(name)) {
found = true;
}
if (name.indexOf(' ') > -1) {
if (name.replace(/\s/g, '').indexOf(needle) === 0) {
found = true;
}
}
return found
}
任何幫助將不勝感激。先謝謝你!
我已經更新了評論向你展示寫作的功能,我認爲對我來說更容易分解它 – user3299613