Replace words in the body text

JavascriptHtml

Javascript Problem Overview


Is there a way to replace the normal text within a table element that is placed within the body of the HTML?

Like replacing "hello" with "hi"?

Please only use JavaScript without jQuery.

Javascript Solutions


Solution 1 - Javascript

To replace a string in your HTML with another use the replace method on innerHTML:

document.body.innerHTML = document.body.innerHTML.replace('hello', 'hi');

Note that this will replace the first instance of hello throughout the body, including any instances in your HTML code (e.g. class names etc..), so use with caution - for better results, try restricting the scope of your replacement by targeting your code using document.getElementById or similar.

To replace all instances of the target string, use a simple regular expression with the global flag:

document.body.innerHTML = document.body.innerHTML.replace(/hello/g, 'hi');

Solution 2 - Javascript

I ended up with this function to safely replace text without side effects (so far):

function replaceInText(element, pattern, replacement) {
    for (let node of element.childNodes) {
        switch (node.nodeType) {
            case Node.ELEMENT_NODE:
                replaceInText(node, pattern, replacement);
                break;
            case Node.TEXT_NODE:
                node.textContent = node.textContent.replace(pattern, replacement);
                break;
            case Node.DOCUMENT_NODE:
                replaceInText(node, pattern, replacement);
        }
    }
}

It's for cases where the 16kB of findAndReplaceDOMText are a bit too heavy.

Solution 3 - Javascript

The function below works perfectly for me:

// Note this *is* JQuery, see below for JS solution instead
function replaceText(selector, text, newText, flags) {
  var matcher = new RegExp(text, flags);
  $(selector).each(function () {
    var $this = $(this);
    if (!$this.children().length)
       $this.text($this.text().replace(matcher, newText));
  });
}

Here's a usage example:

function replaceAllText() {
  replaceText('*', 'hello', 'hi', 'g');
}

$(document).ready(replaceAllText);
$('html').ajaxStop(replaceAllText);

You can also use do a direct replacement like so:

document.body.innerHTML = document.body.innerHTML.replace('hello', 'hi');

But be careful with it since it may affect the tags, css and scripts as well.

EDIT: As for a pure JavaScript solution use this method instead:

function replaceText(selector, text, newText, flags) {
  var matcher = new RegExp(text, flags);
  var elems = document.querySelectorAll(selector), i;

  for (i = 0; i < elems.length; i++)
    if (!elems[i].childNodes.length)
      elems[i].innerHTML = elems[i].innerHTML.replace(matcher, newText);
}

Solution 4 - Javascript

I had the same problem. I wrote my own function using replace on innerHTML, but it would screw up anchor links and such.

To make it work correctly I used a library to get this done.

The library has an awesome API. After including the script I called it like this:

findAndReplaceDOMText(document.body, {
  find: 'texttofind',
  replace: 'texttoreplace'
  }
);

Solution 5 - Javascript

Sometimes changing document.body.innerHTML breaks some JS scripts on page. Here is version, that only changes content of text elements:

function replaceRecursively(element, from, to) {
	if (element.childNodes.length) {
		element.childNodes.forEach(child => replaceRecursively(child, from, to));
	} else {
		const cont = element.textContent;
		if (cont) element.textContent = cont.replace(from, to);
	}
};

replaceRecursively(document.body, new RegExp("hello", "g"), "hi");

Solution 6 - Javascript

I was trying to replace a really large string and for some reason regular expressions were throwing some errors/exceptions.

So I found this alternative to regular expressions which also runs pretty fast. At least it was fast enough for me:

var search = "search string";
var replacement = "replacement string";

document.body.innerHTML = document.body.innerHTML.split(search).join(replacement)

src: https://stackoverflow.com/questions/1144783/how-to-replace-all-occurrences-of-a-string-in-javascript

Solution 7 - Javascript

While using innerHTML.replace will work and is pretty fast, this will break the DOM state.

You can replicate this by setting "autofocus" on an input field and then changing innerHTML on body, it will loose focus.

A less destructive approach is:

// retrives all childNodes of body
var childNodes = document.body.childNodes;

// start replacing
replaceInNodes(childNodes, "search", "replace");

function replaceInNodes(nodes,search,replace) {
	// iterate through all nodes
	for (var i = 0; i < nodes.length; i++) { 
		var curNode = nodes[i];
		// if the node has attributes, let us look at those
		// i.e. we want to change "John" in the input placeholder to "Peter" - <input type="text" value="John">
		if (curNode.attributes !== undefined) {
			var curNodeAttributes = curNode.attributes;
			for (var ii = 0; ii < curNodeAttributes.length; ii++) {
				// replace attribute values
				curNodeAttributes[ii].nodeValue = curNodeAttributes[ii].nodeValue.replace(search, replace);
			}	
		}
		// It is a "TEXT_NODE"
		// i.E. <span>John</span>
		if (curNode.nodeType === Node.TEXT_NODE) {
			curNode.data = this.injectIntoString(curNode.data);
		}
		// It is a "ELEMENT_NODE", meaning we need to go deeper
		if (curNode.nodeType === Node.ELEMENT_NODE) {
			this.replaceInNodes(curNode.childNodes);
		}
	}
}

More info can be found here: https://zgheb.com/i?v=blog&pl=71#12.11.2020_-_Unobstrusively_replace_text_with_JavaScript

Note: I am the author of said link

Solution 8 - Javascript

Use the default javascript string replace function

var curInnerHTML = document.body.innerHTML;
curInnerHTML = curInnerHTML.replace("hello", "hi");
document.body.innerHTML = curInnerHTML;

Solution 9 - Javascript

Try to apply the above suggested solution on pretty big document, replacing pretty short strings which might be present in innerHTML or even innerText, and your html design becomes broken at best

Therefore I firstly pickup only text node elements via HTML DOM nodes, like this

function textNodesUnder(node){
  var all = [];
  for (node=node.firstChild;node;node=node.nextSibling){
    if (node.nodeType==3) all.push(node);
    else all = all.concat(textNodesUnder(node));
  }
  return all;
}

textNodes=textNodesUnder(document.body)
for (i in textNodes) { textNodes[i].nodeValue = textNodes[i].nodeValue.replace(/hello/g, 'hi');    

`and followingly I applied the replacement on all of them in cycle

Solution 10 - Javascript

Wanted to add an example to funky-future's answer as I kept getting this error:

Uncaught TypeError: element.childNodes is not iterable

use like this:

replaceInText(document.body,"search term","replacement");

it didn't work for me with jQuery selectors.

Solution 11 - Javascript

I am new to Javascript and just started learning these skills. Please check if the below method is useful to replace the text.

<script>

	var txt=document.getElementById("demo").innerHTML;
	var pos = txt.replace(/Hello/g, "hi")
	document.getElementById("demo").innerHTML = pos;

</script>

Attributions

All content for this solution is sourced from the original question on Stackoverflow.

The content on this page is licensed under the Attribution-ShareAlike 4.0 International (CC BY-SA 4.0) license.

Content TypeOriginal AuthorOriginal Content on Stackoverflow
QuestionWahteverView Question on Stackoverflow
Solution 1 - JavascriptDexterView Answer on Stackoverflow
Solution 2 - Javascriptfunky-futureView Answer on Stackoverflow
Solution 3 - JavascriptZiadView Answer on Stackoverflow
Solution 4 - JavascriptDavid Silva SmithView Answer on Stackoverflow
Solution 5 - JavascriptOleksandr BoikoView Answer on Stackoverflow
Solution 6 - JavascriptBartho BernsmannView Answer on Stackoverflow
Solution 7 - Javascriptozzi-View Answer on Stackoverflow
Solution 8 - JavascriptMark CostelloView Answer on Stackoverflow
Solution 9 - JavascriptFantomX1View Answer on Stackoverflow
Solution 10 - JavascriptY StroliView Answer on Stackoverflow
Solution 11 - JavascriptManishView Answer on Stackoverflow