<template>
<div v-html="highlightedText"></div>
</template>
<script lang="ts">
import { defineComponent, computed } from 'vue';
export default defineComponent({
name: 'TextHighlight',
props: {
text: {
type: String,
required: true,
},
highlight: {
type: String,
required: true,
},
},
setup(props) {
const highlightedText = computed(() => {
if (!props.highlight) {
return props.text;
}
// Escape special characters in the highlight string
const escapedHighlight = props.highlight.replace(/[.*+?^${}()|[\]\\]/g, '\\$&');
const regex = new RegExp(`(${escapedHighlight})`, 'gi');
return props.text.replace(regex, '<b>$1</b>');
});
return {
highlightedText,
};
},
});
</script>
<style scoped>
b {
font-weight: bold;
}
</style>
<template>
<div>
<TextHighlight text="Это пример текста для подсветки" highlight="прим" />
</div>
</template>
<script>
import TextHighlight from './components/TextHighlight.vue';
export default {
components: {
TextHighlight,
},
};
</script>