Skip to content

Instantly share code, notes, and snippets.

@lewdev
Last active September 23, 2024 05:51
Show Gist options
  • Select an option

  • Save lewdev/53558dd5e86a12f3b98393a2a74f229f to your computer and use it in GitHub Desktop.

Select an option

Save lewdev/53558dd5e86a12f3b98393a2a74f229f to your computer and use it in GitHub Desktop.
πŸ” Fuzzy Search using `RegExp`
<input oninput="fuzzySearch(this.value)">
<ul id=o></ul>
<script>
const list = Object.keys(window);
const fuzzySearch = searchValue => {
let buf = ".*" + searchValue.replace(/[.*+?^${}()|[\]\\]/g, '\\$&').replace(/(.)/g, "$1.*").toLowerCase();
var reg = new RegExp(buf);
let newList = list.filter(e => reg.test(e.toLowerCase()));
o.innerHTML = newList.map(s => `<li>${s}</li>`).join``;
};
fuzzySearch("");
</script>

πŸ” Fuzzy Search using RegExp

Found a fuzzy search implementation using RegExp on StackOverflow.

I fixed it's only flaw of breaking when using RegExp characters.

This code fuzzy searches the attributes found in the window object. It's not great, but a small solution to the problem.

I couldn't find any other small solutions to it yet.

<input oninput="fuzzySearch(this.value)">
<ul id=o></ul>
<script>
const list = Object.keys(window);
const fuzzySearch = searchValue => {
  let buf = ".*" + searchValue.replace(/[.*+?^${}()|[\]\\]/g, '\\$&').replace(/(.)/g, "$1.*").toLowerCase();
  var reg = new RegExp(buf);
  let newList = list.filter(e => reg.test(e.toLowerCase()));
  o.innerHTML = newList.map(s => `<li>${s}</li>`).join``;
};
fuzzySearch("");
</script>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment