A React component for mark and highlighting words within text with flexible word boundary matching and customizable styling.
- 🎯 Precise word boundary matching (whole word, start, end)
- 🎨 Customizable styling and components
- 🛡️ Special characters escaping
- 🔧 Custom match function support
npm i react-mark-words
import { MarkWords } from 'react-mark-words'
function App() {
return (
<MarkWords
text="Hello World! This is a test message."
words={['Hello', 'test']}
/>
)
}
import { MarkWords } from 'react-mark-words'
function App() {
return (
<MarkWords
text="Hello World! This is a test message."
words={['Hello', 'test']}
escape={true} // escape RegExp special characters
caseSensitive={false} // case-sensitive matching
boundary={true} // whole word matching
className="mark-words" // container className
classNames={{
marked: 'custom-marked',
unmarked: 'custom-unmarked',
}}
containerTag="p"
unmarkedTag={({ children, className }) => {
return (
<span
className={className} // custom-unmarked
>{children}</span>
)
}} // custom unmarked component
markedTag={({ children, className }) => {
return (
<HoverCard>
<HoverCardTrigger
className={className} // custom-marked
>
{children}
</HoverCardTrigger>
<HoverCardContent>
Content
</HoverCardContent>
</HoverCard>
)
}}
/>
)
}
The main component for marking words within text.
Type: string
The text content to be processed
Type: string[]
Array of words or phrases to be marked
Type: string?
Optional className for the container element
Type: { marked: string, unmarked: string }?
Optional classNames for marked and unmarked text segments
Type: boolean?
Whether to escape special RegExp characters in search words. Default: true
Type: boolean?
Whether the search should be case-sensitive. Default: false
Type: boolean?
Word boundary matching behavior. Default: true
Type: string | React.ComponentType<any>?
Custom component or HTML tag for marked text. Default:'mark'
Type: string | React.ComponentType<any>?
Custom component or HTML tag for unmarked text. Default: text-only component
Type: string | React.ComponentType<any>?
Container element tag or component. Default: 'div'
Type: (word: string) => RegExp?
Optional custom matching function
MIT © Lee