A Vue 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 vue-mark-words
<template>
<MarkWords
text="Hello World! This is a demo text."
:words="['Hello', 'demo']"
/>
</template>
<template>
<MarkWords
text="Hello World! This is a demo text."
:words="['Hello', 'demo']"
:caseSensitive="true"
/>
</template>
<template>
<MarkWords
text="test retest tests"
:words="['test']"
:boundary="true" // match "test"
boundary="start" // match "tests"
boundary="end" // match "retest"
/>
</template>
<template>
<MarkWords
text="Hello World! This is a demo text."
:words="['Hello', 'demo']"
markedTag="strong"
unmarkedTag="span"
containerTag="p"
/>
</template>
<template>
<MarkWords
text="Hello World! This is a demo text."
:words="['Hello', 'demo']"
:match="(word: string) => new RegExp(`\\b${word}\\b`, 'gi')"
/>
</template>
<template>
<MarkWords
text="Hello World! This is a demo text."
:words="['Hello', 'demo']"
:classNames="{
marked: 'custom-marked',
unmarked: 'custom-unmarked',
container: 'mark-words-container',
}"
/>
</template>
<style>
.custom-marked {
background-color: yellow;
font-weight: bold;
}
.unmarked {
color: #333;
}
.mark-words-container {
padding: 10px;
}
</style>
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, container: 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 | 'start' | 'end')?
Word boundary matching behavior. Default: true
Type: (string | Component)?
Custom component or HTML tag for marked text. Default:'mark'
Type: (string | Component)?
Custom component or HTML tag for unmarked text. Default: text-only component
Type: (string | Component)?
Container element tag or component. Default: 'div'
Type: (word: string) => RegExp?
Optional custom matching function
MIT © Lee