Skip to content

Instantly share code, notes, and snippets.

@amitasaurus
Last active April 30, 2024 10:50
Show Gist options
  • Save amitasaurus/a53ed662a0f50f168994066b94a195ec to your computer and use it in GitHub Desktop.
Save amitasaurus/a53ed662a0f50f168994066b94a195ec to your computer and use it in GitHub Desktop.
Given a content string and a query string, implement a function textSearch that finds all case-insensitive matches with the query string, wrapping the matches in <b>...</b> tags.
export default function textSearch(text: string, query: string): string {
if (query.length === 0) return text;
const regex = new RegExp(query, 'gi');
const matches = Array.from(text.matchAll(regex));
console.log(matches);
let offset = 0;
if (matches.length === 0) return text;
matches.forEach((match) => {
const textString = text;
const index = match.index + offset;
const queryString = textString.substring(index, index + query.length);
text =
textString.slice(0, index) +
`<b>${queryString}</b>` +
textString.slice(index + queryString.length);
offset += '<b></b>'.length;
});
return text;
}
console.log(textSearch('The Quick Brown Fox Jumps Over The Lazy Dog', 'fox'));
// 'The Quick Brown <b>Fox</b> Jumps Over The Lazy Dog'
console.log(textSearch('The hardworking Dog overtakes the lazy dog', 'dog'));
// 'The hardworking <b>Dog</b> overtakes the lazy <b>dog</b>'
console.log(textSearch('The Quick Brown Fox Jumps Over The Lazy Dog', 'jump'));
export default function textSearch(
text: string,
queries: Array<string>
): string {
let offset = 0;
if (queries.length === 0) return text;
queries.forEach((query) => {
if (query.length > 0) {
const regex = new RegExp(query, 'gi');
const matches = Array.from(text.matchAll(regex));
matches.forEach((match, i) => {
const index = match.index + offset;
const start = text.substr(0, index);
const queryStr = `<b>${text.slice(index, index + query.length)}</b>`;
const end = text.substring(index + query.length);
text = start + queryStr + end;
offset += `<b></b>`.length;
});
}
});
text = text.replaceAll(new RegExp('</b><b>', 'gi'), '');
return text;
}
// console.log(textSearch('The Quick Brown Fox Jumps Over The Lazy Dog', ['fox']));
// 'The Quick Brown <b>Fox</b> Jumps Over The Lazy Dog'
// console.log(textSearch('The quick brown fox jumps over the lazy dog', ['fox', 'dog']));
// 'The quick brown <b>fox</b> jumps over the lazy <b>dog</b>'
console.log(textSearch('aabbcc', ['a']));
//'<b>aa</b>bbcc'
// console.log(textSearch('', [''])); // ''
@amitasaurus
Copy link
Author

In browsers, we are able to find specific words or phrases within a webpage by using Ctrl + F (Windows, Linux) or ⌘ + F (Mac) and entering the search term. Matches which appear will be highlighted in yellow.

Let's implement a simple version of a browser's in-webpage search with the difference being we're given a string (as opposed to HTML) and search matches appear bolded.

Given a content string and a query string, implement a function textSearch that finds all case-insensitive matches with the query string, wrapping the matches in ... tags.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment