You can use the text method and pass a function that returns the modified text, using the native String.prototype.replace method to perform the replacement:
$(“.text_div”).text(function () {
return $(this).text().replace(“contains”, “hello everyone”);
});
Here’s a working example.
If it’s possible, you could wrap the word(s) you want to replace in a span tag, like so:
This div contains some text.
You can then easily change its contents with jQuery:
$(‘.text_div > span’).text(‘hello everyone’);
If you can’t wrap it in a span tag, you could use regular expressions.