I'm building an application where users highlight and scroll to words in an article they write in search bar. articles come in a Markdown format and I'm using a Markdown-it to render article body.
It works well except for if the word they search for is part of an image URL. it applies regular expression to it and image breaks.
applyHighlights() {
let str = this.article.body
let searchText = this.articleSearchAndLocate
const regex = new RegExp(searchText, 'gi')
let text = str
if (this.articleSearchAndLocate == '') {
return text
} else {
const newText = text.replace(
regex,
`<span id="searchResult" class="rounded-sm shadow-xl py-0.25 px-1 bg-accent font-semibold text-tint">$&</span>`
)
return newText
}
}
Is there a way to exclude applying regex if it's an Image URL ?