- 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/
如何使用 SPListCollection.Add(String, String, String, String, Int32, String, SPListTemplate.QuickLaunchO
我刚刚开始使用 C++ 并且对 C# 有一些经验,所以我有一些一般的编程经验。然而,似乎我马上就被击落了。我试过在谷歌上寻找,以免浪费任何人的时间,但没有结果。 int main(int argc,
这个问题已经有答案了: In Java 8 how do I transform a Map to another Map using a lambda? (8 个回答) Convert a Map>
我正在使用 node + typescript 和集成的 swagger 进行 API 调用。我 Swagger 提出以下要求 http://localhost:3033/employees/sear
我是 C++ 容器模板的新手。我收集了一些记录。每条记录都有一个唯一的名称,以及一个字段/值对列表。将按名称访问记录。字段/值对的顺序很重要。因此我设计如下: typedef string
我需要这两种方法,但j2me没有,我找到了一个replaceall();但这是 replaceall(string,string,string); 第二个方法是SringBuffer但在j2me中它没
If string is an alias of String in the .net framework为什么会发生这种情况,我应该如何解释它: type JustAString = string
我有两个列表(或字符串):一个大,另一个小。 我想检查较大的(A)是否包含小的(B)。 我的期望如下: 案例 1. B 是 A 的子集 A = [1,2,3] B = [1,2] contains(A
我有一个似乎无法解决的小问题。 这里...我有一个像这样创建的输入... var input = $(''); 如果我这样做......一切都很好 $(this).append(input); 如果我
我有以下代码片段 string[] lines = objects.Split(new string[] { "\r\n", "\n" }, StringSplitOptions.No
这可能真的很简单,但我已经坚持了一段时间了。 我正在尝试输出一个字符串,然后输出一个带有两位小数的 double ,后跟另一个字符串,这是我的代码。 System.out.printf("成本:%.2
以下是 Cloud Firestore 列表查询中的示例之一 citiesRef.where("state", ">=", "CA").where("state", "= 字符串,我们在Stack O
我正在尝试检查一个字符串是否包含在另一个字符串中。后面的代码非常简单。我怎样才能在 jquery 中做到这一点? function deleteRow(locName, locID) { if
这个问题在这里已经有了答案: How to implement big int in C++ (14 个答案) 关闭 9 年前。 我有 2 个字符串,都只包含数字。这些数字大于 uint64_t 的
我有一个带有自定义转换器的 Dozer 映射: com.xyz.Customer com.xyz.CustomerDAO customerName
这个问题在这里已经有了答案: How do I compare strings in Java? (23 个回答) 关闭 6 年前。 我想了解字符串池的工作原理以及一个字符串等于另一个字符串的规则是
我已阅读 this问题和其他一些问题。但它们与我的问题有些无关 对于 UILabel 如果你不指定 ? 或 ! 你会得到这样的错误: @IBOutlet property has non-option
这两种方法中哪一种在理论上更快,为什么? (指向字符串的指针必须是常量。) destination[count] 和 *destination++ 之间的确切区别是什么? destination[co
This question already has answers here: Closed 11 years ago. Possible Duplicates: Is String.Format a
我有一个Stream一个文件的,现在我想将相同的单词组合成 Map这很重要,这个词在 Stream 中出现的频率. 我知道我必须使用 collect(Collectors.groupingBy(..)
我是一名优秀的程序员,十分优秀!