Replace words in body text

Is there a way to replace plain text inside a table element that is placed in the body of the HTML?

How to replace hello with hello?

Use JavaScript only jQuery .

+52
javascript html
Apr 05 2018-11-21T00:
source share
6 answers

To replace your HTML string with another, use the replace innerHTML method.

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

Please note that this will replace the first instance of hello throughout the body, including any instances of your HTML code (for example, class names, etc.), so use with caution - for best results, try limiting your scope with document.getElementById or similar.

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

 document.body.innerHTML = document.body.innerHTML.replace(/hello/g, 'hi'); 
+84
Apr 05 2018-11-21T00:
source share

The function below works fine 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 is a usage example:

 function replaceAllText() { replaceText('*', 'hello', 'hi', 'g'); } $(document).ready(replaceAllText); $('html').ajaxStop(replaceAllText); 

You can also use direct replacement like this:

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

But be careful, as this can affect tags, css and scripts.

EDIT: Regarding a clean JavaScript solution, use this method:

 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); } 
+8
Sep 06 '14 at 10:03
source share

I had the same problem. I wrote my own function, replacing it with innerHTML, but it will stick anchor links, etc.

To make it work correctly, I used library to do this.

There is an awesome API in the library. After turning on the script, I called it the following:

 findAndReplaceDOMText(document.body, { find: 'texttofind', replace: 'texttoreplace' } ); 
+3
Feb 11 '15 at 9:31 on
source share

I tried to replace a really big string, and for some reason regular expressions threw some errors / exceptions.

So, I found this alternative to regular expressions, which also run 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: How to replace all occurrences of a string in JavaScript?

0
Apr 20 '17 at 7:16
source share

Use default javascript string replacement function

 var curInnerHTML = document.body.innerHTML; curInnerHTML = curInnerHTML.replace("hello", "hi"); document.body.innerHTML = curInnerHTML; 
-one
Apr 05 2018-11-11T00:
source share

I am new to Javascript and have just begun to learn these skills. Check if the method below is suitable for replacing text.

 <script> var txt=document.getElementById("demo").innerHTML; var pos = txt.replace(/Hello/g, "hi") document.getElementById("demo").innerHTML = pos; </script> 
-one
Jul 12 '16 at 4:26
source share



All Articles