Regex匹配可能或可能不以“”开头的字符串;“"A“;或“;一个“;

Regex Match String That May or May Not Start with "The," "A" or "An"

本文关键字:quot 一个 字符串 Regex 开头      更新时间:2023-09-26

我正在开发一个波段搜索引擎,我想制作它,这样用户就不需要键入文章来查找波段。因此,例如,如果有人想找到"黑钥匙",他们可以通过键入"Bla"、"Black Key"、"The Black Keys"或任何类似的变体来找到它们。以下是我目前所拥有的:

matcher = new RegExp( '^(?<=the)'+searchstring, 'i' );

这是一个开始:

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|a)-您的文章列表
  • -单词之间的空格
  • CCD_5关闭非捕获组并将其设为可选

工作小提琴

相关文章: