From f10a7e1f7e6b4b375c165d56f3ac06ecfdffd76b Mon Sep 17 00:00:00 2001 From: George Kalpakas Date: Sun, 18 Apr 2021 16:14:48 +0300 Subject: [PATCH] refactor(docs-infra): simplify regex in search WebWorker (#41693) This commit simplifies a regex used in angular.io's search WebWorker. It also updates some comments to add more context on what the code does. PR Close #41693 --- aio/src/app/search/search.worker.ts | 5 +++-- 1 file changed, 3 insertions(+), 2 deletions(-) diff --git a/aio/src/app/search/search.worker.ts b/aio/src/app/search/search.worker.ts index 0874763303..f1c44bc30b 100644 --- a/aio/src/app/search/search.worker.ts +++ b/aio/src/app/search/search.worker.ts @@ -119,7 +119,8 @@ function queryIndex(query: string): PageInfo[] { try { if (query.length) { // First try a query where every term must be present - const queryAll = query.replace(/(^|\s)([^\s]+)/g, '$1+$2'); + // (see https://lunrjs.com/guides/searching.html#term-presence) + const queryAll = query.replace(/\S+/g, '+$&'); let results = index.search(queryAll); // If that was too restrictive just query for any term to be present @@ -129,7 +130,7 @@ function queryIndex(query: string): PageInfo[] { // If that is still too restrictive then search in the title for the first word in the query if (results.length === 0) { - // E.g. if the search is "ngCont guide" then we search for "ngCont guide title:ngCont*" + // E.g. if the search is "ngCont guide" then we search for "ngCont guide title:*ngCont*" const titleQuery = 'title:*' + query.split(' ', 1)[0] + '*'; results = index.search(query + ' ' + titleQuery); }