To achieve this, we need to replace a character match with <strong>{input character}</strong>
in a particular sentence.
So Here is the function:
const boldMatchCharacters = ({ sentence = "", characters = "" }) => {
const regEx = new RegExp(characters, 'gi');
return sentence.replace(regEx, '<strong>$&</strong>')
}
I create regex from
input characters
Using string's replace function, I replace
$&
which returns the whole match as it is with<strong>$&</strong>
Example
console.log(boldMatchCharacters({ sentence: 'Hello Hello Hello, How are you?', characters: 'hel' }));
Output
<strong>Hel</strong>lo <strong>Hel</strong>lo <strong>Hel</strong>lo, How are you?
Top comments (1)
Its a good website google to visit more information .