Im getting objects from a backend with a search request. Now i want to highlight the searched term when i display my object.
For example if my search term is "mirr" and in my object there is a name field called mirror
{name: "mirror"}
i want to wrap something like <span class="text-red-400">mirr<span>or
about the searched word.
Is there anyway to achieve this without the usage of jquerys addClass function? Maybe there is some regex solution here? Sorry im not familiar with regex and its syntax...
I already tried it with something like myObject.replace(searchQuery, "<span class='text-red-400'>" + searchQuery + "</span>")
but its not working.
If this is not working with tailwind im also open for css solutions as well.
Edit: I get some results with the following solution:
hightlight(){
let template = this.$refs.myComponent.outerHTML
template = template.replace(
new RegExp(this.query, 'gi'),
(match) => `<span class="myClass">${match}</span>`
)
this.$refs.myComponent.innerHTML= template
}
but the problem with this solution is, that also the html tags get replaced. For example if i search for "spa", then also my elements get replaced. I want to exclude every html from the replacing. For example if i want to search for "sparta" and i actually type in "spa" in my search, this should be the result <span><span class="myClass">spa</span>rta</span>