diff options
author | Martin Fischer <martin@push-f.com> | 2022-07-21 12:43:04 +0200 |
---|---|---|
committer | Martin Fischer <martin@push-f.com> | 2022-07-21 12:43:08 +0200 |
commit | 6178d0000036d9dee63639a420aa510504216345 (patch) | |
tree | 1025e11bbccc480b3e72b24faadf2ca8089e6639 /script.js | |
parent | 8b28d1bb96fa2da47a44aa3b2d38544421ff8b51 (diff) |
improve search results by requiring word boundary
E.g. searching for "sport" should not yield proposals about transport.
Diffstat (limited to 'script.js')
-rw-r--r-- | script.js | 7 |
1 files changed, 6 insertions, 1 deletions
@@ -21,10 +21,15 @@ function normalize(str) { return str.toLowerCase().replaceAll('_', ' ') } +function escapeForRegex(str) { + return str.replace(/[.*+?^${}()|[\]\\]/g, '\\$&') +} + function display(proposals) { const params = {}; if (nameInput.value) { - proposals = proposals.filter(p => normalize(p.name || p.page_title).includes(normalize(nameInput.value))); + const regex = new RegExp('\\b' + escapeForRegex(normalize(nameInput.value))); + proposals = proposals.filter(p => regex.test(normalize(p.name || p.page_title))); params.q = nameInput.value; } |