У меня есть текстовая область слов. Существует функция jQuery, которая будет l oop по массиву для поиска каждой работы в тексте, если она найдет, поместит ее в массив, а затем отобразит ее. Это хорошо работает. Текстовая область является редактируемой, позволяя пользователю изменять текст и затем выполнять поиск снова (с учетом исправления орфографии или контекста). После изменения текста и повторного выполнения функции он находит только подстроку строки, а не полную, см. Ниже:
Текст для поиска : Я живу по маршруту Q53. Я хочу увидеть маршрут Q3 возле моего дома.
Если вы добавили Q44 в конце после периода, он найдет только Q4 ..
$(document).ready(function() {
findRoutes();
});
function findRoutes(){
$('#route_list').html('');
var found_routes = [];
var rts = ["Q1","Q2","Q3","Q4","Q5","Q6","Q7","Q8","Q9","Q10","Q11","Q12","Q13","Q15","Q15A","Q16","Q17","Q18","Q19","Q20A","Q20B","Q21","Q22","Q23","Q24","Q25","Q26","Q27","Q28","Q29","Q30","Q31","Q32","Q33","Q34","Q35","Q36","Q37","Q38","Q39","Q40","Q41","Q42","Q43","Q44-SBS","Q46","Q47","Q48","Q49","Q50","Q52-SBS","Q53-SBS","Q54","Q55","Q56","Q58","Q59","Q60","Q64","Q65","Q66","Q67","Q69","Q70-SBS","Q72","Q76","Q77","Q83","Q84","Q85","Q88","Q100","Q101","Q102","Q103","Q104","Q110","Q111","Q112","Q113","Q114","QM1","QM2","QM3","QM4","QM5","QM6","QM7","QM8","QM10","QM11","QM12","QM15","QM16","QM17","QM18","QM20","QM21","QM24","QM25","QM31","QM32","QM34","QM35","QM36","QM40","QM42","QM44","X63","X64","X68","QMT100","QMT101","QMT102","QMT103","QMT104","QMT105","QMT106","QMT107","QMT112","QMT115","QMT116","QMT117","QMT130","QMT131","QMT132","QMT133","QMT134","QMT135","QMT155","QMT156","QMT157","QMT160","QMT161","QMT162","QMT163","QMT164","QMT165","QMT166","QMT167","QMT168","QMT169","QMT170","M60","QT10","QT11","QT12","QT13","QT14","QT15","QT16","QT17","QT18","QT19","QT1","QT20","QT22","QT24","QT2","QT30","QT31","QT32","QT33","QT34","QT35","QT36","QT37","QT38","QT39","QT3","QT40","QT41","QT42","QT43","QT44","QT45","QT46","QT47","QT48","QT49","QT4","QT50","QT51","QT52","QT54","QT55","QT56","QT58","QT59","QT5","QT60","QT61","QT62","QT63","QT64","QT65","QT66","QT67","QT68","QT69","QT6","QT70","QT71","QT72","QT73","QT74","QT75","QT76","QT77","QT78","QT79","QT7","QT80","QT81","QT82","QT83","QT84","QT85","QT86","QT87","QT88"];
var comment = $("#comment_div").val();
jQuery.each(rts, function(index, item) {
// if (comment.indexOf(item) >= 0) {
if (comment.includes( item )) {
found_routes.push(item);
}
});
jQuery.each(found_routes, function(index, item) {
$('#route_list').append(item+"<br>");
});
var final_routes = found_routes.toString();
$("#string_routes").val(final_routes);
}
См. fiddle для полного примера. Спасибо за помощь.