簡體   English   中英

正則表達式匹配字符串,可以以“ The”,“ A”或“ An”開頭或不以

[英]Regex Match String That May or May Not Start with “The,” “A” or “An”

我正在研究樂隊的搜索引擎,並且希望使其成為一個樂隊,以便用戶無需鍵入文章即可找到樂隊。 因此,例如,如果有人想找到“黑鍵”,則可以通過鍵入“ Bla”,“黑鍵”,“黑鍵”或類似的任何形式來找到它們。 這是我到目前為止的內容:

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) -您的文章列表
  • -單詞之間的間隔
  • )? 關閉非捕獲組並將其設置為可選。

工作的小提琴

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM