简体   繁体   中英

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 ? -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. I only want the string that starts with 'ab' and not where it comes in the middle.

So I modified my regexp like this '\\^ab.*t$\\ but still the same result. So can somebody give me some tips on how this can be achieved.

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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