[英]Regex Match String That May or May Not Start with “The,” “A” or “An”
I am working on a search engine for bands and I want to make it so that the user doesn't need to type the article to find the band. 我正在研究乐队的搜索引擎,并且希望使其成为一个乐队,以便用户无需键入文章即可找到乐队。 So, for example, if someone wanted to find "The Black Keys," they could find them by typing "Bla," "Black Keys," "The Black Keys," or any variation like that.
因此,例如,如果有人想找到“黑键”,则可以通过键入“ Bla”,“黑键”,“黑键”或类似的任何形式来找到它们。 Here is what I have so far:
这是我到目前为止的内容:
matcher = new RegExp( '^(?<=the)'+searchstring, 'i' );
This is a start: 这是一个开始:
var tags = ["c++", "java", "the php", "coldfusion", "a javascript", "asp", "the ruby"];
$("#autocomplete").autocomplete({
source: function (request, response) {
var matcher = new RegExp("^(?:(the|a) )?" + $.ui.autocomplete.escapeRegex(request.term), "i");
response($.grep(tags, function (item) {
return matcher.test(item);
}));
}
});
The regex is modified to: 正则表达式修改为:
^
- start of word ^
-字词开头 (?:
- begin a non capturing group (?:
-开始一个非捕获组 (the|a)
- your list of articles (the|a)
-您的文章列表
- the space between words )?
close non capture group and make it optional.
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.