- html - 出于某种原因,IE8 对我的 Sass 文件中继承的 html5 CSS 不友好?
- JMeter 在响应断言中使用 span 标签的问题
- html - 在 :hover and :active? 上具有不同效果的 CSS 动画
- html - 相对于居中的 html 内容固定的 CSS 重复背景?
我有一个单词文本区域。有一个 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/
我正在尝试创建一个包含 int[][] 项的数组 即 int version0Indexes[][4] = { {1,2,3,4}, {5,6,7,8} }; int version1Indexes[
我有一个整数数组: private int array[]; 如果我还有一个名为 add 的方法,那么以下有什么区别: public void add(int value) { array[va
当您尝试在 JavaScript 中将一个数组添加到另一个数组时,它会将其转换为一个字符串。通常,当以另一种语言执行此操作时,列表会合并。 JavaScript [1, 2] + [3, 4] = "
根据我正在阅读的教程,如果您想创建一个包含 5 列和 3 行的表格来表示这样的数据... 45 4 34 99 56 3 23 99 43 2 1 1 0 43 67 ...它说你可以使用下
我通常使用 python 编写脚本/程序,但最近开始使用 JavaScript 进行编程,并且在使用数组时遇到了一些问题。 在 python 中,当我创建一个数组并使用 for x in y 时,我得
我有一个这样的数组: temp = [ 'data1', ['data1_a','data1_b'], ['data2_a','data2_b','data2_c'] ]; // 我想使用 toStr
rent_property (table name) id fullName propertyName 1 A House Name1 2 B
这个问题在这里已经有了答案: 关闭13年前。 Possible Duplicate: In C arrays why is this true? a[5] == 5[a] array[index] 和
使用 Excel 2013。经过多年的寻找和适应,我的第一篇文章。 我正在尝试将当前 App 用户(即“John Smith”)与他的电子邮件地址“jsmith@work.com”进行匹配。 使用两个
当仅在一个边距上操作时,apply 似乎不会重新组装 3D 数组。考虑: arr 1),但对我来说仍然很奇怪,如果一个函数返回一个具有尺寸的对象,那么它们基本上会被忽略。 最佳答案 这是一个不太理
我有一个包含 GPS 坐标的 MySQL 数据库。这是我检索坐标的部分 PHP 代码; $sql = "SELECT lat, lon FROM gps_data"; $stmt=$db->query
我需要找到一种方法来执行这个操作,我有一个形状数组 [批量大小, 150, 1] 代表 batch_size 整数序列,每个序列有 150 个元素长,但在每个序列中都有很多添加的零,以使所有序列具有相
我必须通过 url 中的 json 获取文本。 层次结构如下: 对象>数组>对象>数组>对象。 我想用这段代码获取文本。但是我收到错误 :org.json.JSONException: No valu
enter code here- (void)viewDidLoad { NSMutableArray *imageViewArray= [[NSMutableArray alloc] init];
知道如何对二维字符串数组执行修剪操作,例如使用 Java 流 API 进行 3x3 并将其收集回相同维度的 3x3 数组? 重点是避免使用显式的 for 循环。 当前的解决方案只是简单地执行一个 fo
已关闭。此问题需要 debugging details 。目前不接受答案。 编辑问题以包含 desired behavior, a specific problem or error, and the
我有来自 ASP.NET Web 服务的以下 XML 输出: 1710 1711 1712 1713
如果我有一个对象todo作为您状态的一部分,并且该对象包含数组列表,则列表内部有对象,在这些对象内部还有另一个数组listItems。如何更新数组 listItems 中 id 为“poi098”的对
我想将最大长度为 8 的 bool 数组打包成一个字节,通过网络发送它,然后将其解压回 bool 数组。已经在这里尝试了一些解决方案,但没有用。我正在使用单声道。 我制作了 BitArray,然后尝试
我们的数据库中有这个字段指示一周中的每一天的真/假标志,如下所示:'1111110' 我需要将此值转换为 boolean 数组。 为此,我编写了以下代码: char[] freqs = weekday
我是一名优秀的程序员,十分优秀!