简体   繁体   English

使用JavaScript在字符串数组中进行通配符搜索(?,*)

[英]wildcard search (?, *) in a string array using javascript

I am trying to achieve wild card search on a string array using java script Here the wild cards i use are ? 我正在尝试使用Java脚本在字符串数组上实现通配符搜索。在这里,我使用的通配符是? -to represent single char and * to represent multiple char -表示单个字符,*表示多个字符

here is my string array 这是我的字符串数组

var sample = new Array();
sample[0] = 'abstract';
sample[1] = 'cabinet';
sample[2] = 'computer';

For example i searched for a string 'ab*t' in the array and the regular expression I used for this is '\\ab.*t\\', but the problem is I get both 'abstract' and 'cabinet' as matching strings. 例如,我在数组中搜索了字符串“ ab * t”,而我使用的正则表达式为“ \\ ab。* t \\”,但是问题是我同时将“ abstract”和“ cabinet”作为匹配字符串。 I only want the string that starts with 'ab' and not where it comes in the middle. 我只希望以'ab'开头的字符串,而不是中间的字符串。

So I modified my regexp like this '\\^ab.*t$\\ but still the same result. 所以我像这样修改我的正则表达式'\\ ^ ab。* t $ \\,但结果仍然相同。 So can somebody give me some tips on how this can be achieved. 所以有人可以给我一些有关如何实现的提示。

您正在使用错误的错误斜杠,应该使用正斜杠('/')而不是反斜杠('\\'),这可能会帮助您/^ab.*t$/

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM