Reputation: 957
How to replace a part of a string with a html tag in jQuery?
Say for e.g. <div>Who am i</div>
should be <div><b>Who</b> am i</div>
.
Upvotes: 4
Views: 133
Reputation: 144729
You can use html
method's callback function and replace
method.
$('div').html(function(_, oldHTML){
return oldHTML.replace(/(\w+)/, '<b>$1</b>');
// return oldHTML.replace('Who', '<b>Who</b>');
})
Upvotes: 4