
- Javascript Basics Tutorial
- Javascript - Home
- Javascript - Overview
- Javascript - Syntax
- Javascript - Enabling
- Javascript - Placement
- Javascript - Variables
- Javascript - Operators
- Javascript - If...Else
- Javascript - Switch Case
- Javascript - While Loop
- Javascript - For Loop
- Javascript - For...in
- Javascript - Loop Control
- Javascript - Functions
- Javascript - Events
- Javascript - Cookies
- Javascript - Page Redirect
- Javascript - Dialog Boxes
- Javascript - Void Keyword
- Javascript - Page Printing
- JavaScript Objects
- Javascript - Objects
- Javascript - Number
- Javascript - Boolean
- Javascript - Strings
- Javascript - Arrays
- Javascript - Date
- Javascript - Math
- Javascript - RegExp
- Javascript - HTML DOM
- JavaScript Advanced
- Javascript - Error Handling
- Javascript - Validations
- Javascript - Animation
- Javascript - Multimedia
- Javascript - Debugging
- Javascript - Image Map
- Javascript - Browsers
- JavaScript Useful Resources
- Javascript - Questions And Answers
- Javascript - Quick Guide
- Javascript - Functions
- Javascript - Resources
Fuzzy Search Algorithm in JavaScript
We are required to write a JavaScript String function that takes in a search string can loosely checks for the search string in the string it is used with.
The function should take this criterion into consideration: It should loop through the search query letters and check if they occur in the same order in the string.
For example −
('a haystack with a needle').fuzzySearch('hay sucks'); // false ('a haystack with a needle').fuzzySearch('sack hand'); // true
Example
const fuzzySearch = function (query) { const str = this.toLowerCase(); let i = 0, n = -1, l; query = query.toLowerCase(); for (; l = query[i++] ;){ if (!~(n = str.indexOf(l, n + 1))){ return false; }; }; return true; }; String.prototype.fuzzySearch = fuzzySearch; console.log(('a haystack with a needle').fuzzySearch('hay sucks')); console.log(('a haystack with a needle').fuzzySearch('sack hand'));
Output
This will produce the following output −
false true
- Related Articles
- C++ Program to Implement Interpolation Search Algorithm
- C++ Program to Implement a Binary Search Algorithm for a Specific Search Sequence
- Interpolation Search in JavaScript
- Algorithm for matrix multiplication in JavaScript
- Dijkstra's algorithm in Javascript
- The Floyd-Warshall algorithm in Javascript
- Prim's algorithm in Javascript
- Kruskal's algorithm in Javascript
- Search in an array with Binary search using JavaScript
- Binary Search program in JavaScript
- Binary Search Tree in Javascript
- Implementing Linear Search in JavaScript
- Implementing block search in JavaScript
- Difference Between Fuzzy Set and Crisp Set
- Fuzzy Logic and Probability: The Confusing Terms

Advertisements