Modify specific words in a paragraph

Modify specific words in a paragraph

Here is a snippet that I found in the internet that adds a class to specific substring of a paragraph.
Problem is that when the paragraph contains elements that are not words - like an element of type <img> , it eliminates this element completely from the outcome.
Can someone modify it to preserve the <img> element as well?
Thx


jQuery(document).ready( function ($) {

var oldString = 'some words',
newString = '<span class ="orang">some words</span>',
newText = $( "p:contains('some words')" ).text().replace(RegExp(oldString,"gi"),newString);
$( "p:contains('some words')" ).html(newText);

}); // jQuery(document).ready( function ($) {