简体   繁体   English

正则表达式匹配字符串,可以以“ The”,“ A”或“ An”开头或不以

[英]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. 关闭非捕获组并将其设置为可选。

a working fiddle 工作的小提琴

暂无
暂无

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

相关问题 将字符串转换为 integer 可能以或不以零开头 - Convert string to integer that may or may not start with zero 使用RegEx更改大小写,其中字符串中可能有数字 - Change case with RegEx where there may be numbers in the string RegEx 用于开头可能有版本的字符串 - RegEx for the string at the beginning of which there may be a version 正则表达式不匹配具有 10 个连续数字的字符串。 数字可以用空格分隔。 所有其他字符串返回匹配项 - Regex to NOT match a string with 10 consecutive digits. The digits may be separated by white space. All other string return a match 正则表达式可能包含一个字符 - Regex may contain a character RegEx:从可能包含多个“ {”和“}”的字符串中提取子字符串 - RegEx: Extract substring from a string which may contain several number of '{' and '}' 如何匹配可能有或没有后缀的JS正则表达式 - How to match a JS regexp that may or may not have a suffix 当URL可能包含或不包含HTTP和WWW时,URL的JavaScript正则表达式 - JavaScript Regex for URL when the URL may or may not contain HTTP and WWW 正则表达式以匹配HTML标记(可能包含php代码)内的属性 - Regex to match attributes inside an HTML tag which may include php code 如果属性值可能带有“&gt;”字符,则使用JavaScript Regex匹配所有html标签 - Using JavaScript Regex to match all html tags if attribute values may have “>” character
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM