Is there a way in jQuery to find a string of text in jQuery and not replace it with something else but wrap that text with an element so when the script is finished it spits out the original text with the string of text wrapped.
Example:
Original Text
"Hello world to all people"
Search string
"world to"
Replace with <i></i>
Final output
"Hello <i>World to</i> all people"
Thanks in advance for the help!
Kind of working code:
function highlightChild(child) {
$(childElements[child]).text("");
console.log(child);
$('.child_element_' + child).bind('textselect', function(e){
var selection = e.text;
var str = $("#construct_version").text();
var wrap = jQuery(childElements[child]).text(selection);
var re = new RegExp("" + selection + "", "g");
console.log(str.replace(selection, function(match, key, val){
console.log(match);
console.log(key);
console.log(val);
jQuery(childElements[child]).text(val)
}));
});
}
The above code does the replace but where it actually does the replace it shows as undefined.
So if the original string is Hello world to all and I am wanting to replace world to with <b>world to</b>
, it shows in the console.log as Hello undefined all