How Can I Replace Everything In Html Body That Matches A Word?
I have a webpage, like the one below:
123 is a great number!
Solution 1:
Safest way is to walk the dom, and perform the regex on text nodes only:
var regex = /123/g,
replacement = 'Pi';
functionreplaceText(i,el) {
if (el.nodeType === 3) {
if (regex.test(el.data)) {
el.data = el.data.replace(regex, replacement);
}
} else {
$(el).contents().each( replaceText );
}
}
$('body').each( replaceText );
This starts at a root, and recursively calls the replaceText
function on the child nodes, which are obtained using the contents()
method.
If a text node is located, the replace is performed.
Example:http://jsfiddle.net/k6zjT/
Solution 2:
<script>
$(document).ready(function() {
$("body").html(String($('body').html()).replace(/123/g, 'Pi'));
});
</script>
Post a Comment for "How Can I Replace Everything In Html Body That Matches A Word?"