gpt4 book ai didi

javascript - 在数组中搜索文本区域仅查找子字符串

转载 作者:行者123 更新时间:2023-12-01 03:57:53 24 4
gpt4 key购买 nike

我有一个单词文本区域。有一个 jQuery 函数将循环遍历数组来搜索文本中的每个作品,如果找到,则将其放入数组中,然后显示它。这效果很好。文本区域是可编辑的,允许用户修改文本,然后再次搜索(允许更正拼写或上下文)。修改文本并再次执行该函数后,它只找到字符串的子字符串,而不是完整的字符串,如下所示:

搜索文本:我住在 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完整的例子。感谢您的帮助。

最佳答案

要使其按预期工作,您需要确保 Qxx 值完全匹配,而不是部分匹配。为此,您可以使用利用单词边界的正则表达式:

let comment = 'I live on the Q53 Route. I want to see the Q3 route near my house. Q44'
let rts = ["Q1", "Q12", "Q3", "Q33", "Q4", "Q44"];
let found_routes = [];

jQuery.each(rts, function(index, item) {
var re = new RegExp('\\b' + item + '\\b', 'gi');
if (re.test(comment)) {
found_routes.push(item);
}
});

console.log(found_routes);
<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>

或者,如果所有 Qxx 值都遵循 Q 后跟 1 或 2 位数字的相同格式,那么您可以避免循环并使用正则表达式来捕获所有这些:

let comment = 'I live on the Q53 Route. I want to see the Q3 route near my house. Q44'
var re = new RegExp(`\\b(Q\\d{1,2})\\b`, 'gi');
var matches = comment.match(re);
console.log(matches);
<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>

关于javascript - 在数组中搜索文本区域仅查找子字符串,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/60744923/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com