What is highlight-words-core?
The highlight-words-core npm package is a utility for finding and highlighting words within a string. It is particularly useful for search functionalities where you need to emphasize the search terms within the results.
What are highlight-words-core's main functionalities?
Highlight Words
This feature allows you to find and highlight specific words within a given text. The `findAll` function takes an object with `searchWords` and `textToHighlight` properties and returns an array of chunks indicating the positions of the highlighted words.
const { findAll } = require('highlight-words-core');
const textToHighlight = 'This is a sample text to highlight certain words.';
const searchWords = ['sample', 'highlight'];
const chunks = findAll({
searchWords,
textToHighlight
});
console.log(chunks);
Case Insensitive Search
This feature allows you to perform a case-insensitive search. By setting the `caseSensitive` option to `false`, the search will ignore case differences between the search words and the text.
const { findAll } = require('highlight-words-core');
const textToHighlight = 'This is a Sample text to Highlight certain words.';
const searchWords = ['sample', 'highlight'];
const chunks = findAll({
searchWords,
textToHighlight,
autoEscape: true,
caseSensitive: false
});
console.log(chunks);
Escape Special Characters
This feature allows you to escape special characters in the search words. By setting the `autoEscape` option to `true`, special characters in the search words will be escaped, preventing them from being interpreted as regular expressions.
const { findAll } = require('highlight-words-core');
const textToHighlight = 'This is a sample text with special characters like * and ?.';
const searchWords = ['*', '?'];
const chunks = findAll({
searchWords,
textToHighlight,
autoEscape: true
});
console.log(chunks);
Other packages similar to highlight-words-core
react-highlight-words
The react-highlight-words package provides similar functionality but is specifically designed for React applications. It allows you to highlight words within a React component, making it more suitable for front-end development compared to highlight-words-core, which is a more general utility.
highlight.js
The highlight.js package is a syntax highlighter for code, which can also be used to highlight words within a text. However, it is more focused on code syntax highlighting rather than general text highlighting, making it a bit different in scope compared to highlight-words-core.
mark.js
The mark.js package is a versatile library for highlighting text. It offers more advanced features like custom element creation, regular expression support, and more. It is more feature-rich compared to highlight-words-core, which is simpler and more focused on basic word highlighting.