Replace words in the body text
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 g
lobal flag:
document.body.innerHTML = document.body.innerHTML.replace(/hello/g, 'hi');
How do I replace every word in a body of text with another word?
This code:
var text = ' "The quick brown fox jumps over the lazy dog" is an English-language pangram—a phrase that contains all of the letters of the alphabet. It is used to show fonts and to test typewriters and computer keyboards, and in other applications involving all of the letters in the English alphabet. Owing to its brevity and coherence, it has become widely known.'
+ "\n\n" +
' In the age of computers, this pangram is commonly used to display font samples and for testing computer keyboards. Microsoft Word has a command to auto-type the sentence, in versions up to Office 2003, using the command =rand(), and in Office 2007 and later using the command =rand.old().'
+ "\n";
var token = 'bees';
text = text.replace(/\b\w\b/g, token.charAt(0));
text = text.replace(/\b\w{2}\b/g, token.charAt(0) + token.charAt(token.length-1));
text = text.replace(/\b\w{3}\b/g, token.slice(0,2) + token.charAt(token.length-1));
text = text.replace(/\b\w{4,}\b/g, token);
console.log(text);
Got the following output:
"bes bees bees bes bees bees bes bees bes" bs bs bees-bees bees—b bees bees bees bes bs bes bees bs bes bees. bs bs bees bs bees bees bes bs bees bees bes bees bees, bes bs bees bees bees bes bs bes bees bs bes bees bees. bees bs bes bees bes bees, bs bes bees bees bees.
bs bes bes bs bees, bees bees bs bees bees bs bees bees bees bes bes bees bees bees. bees bees bes b bees bs bees-bees bes bees, bs bees bs bs bees bees, bees bes bees =bees(), bes bs bees bees bes bees bees bes bees =bees.bes().
After finding a word in html body, replace it with another one
This would give the result you are looking for (I decided to skip of 'SCRIPT'
nodeName
's so you do not accidentally modify any code:
let word = "active";let queue = [document.body];let curr;
while (curr = queue.pop()) { if (!curr.textContent.match(word) || curr.nodeName === 'SCRIPT') continue; for (var i = 0; i < curr.childNodes.length; ++i) { if (curr.childNodes[i].nodeName === 'SCRIPT') continue; switch (curr.childNodes[i].nodeType) { case Node.TEXT_NODE: // 3 if (curr.childNodes[i].textContent.match(word)) { console.log("Found!", `'${curr.childNodes[i].textContent}'`); curr.childNodes[i].textContent = curr.childNodes[i].textContent.replace('active', 'test'); } break; case Node.ELEMENT_NODE: // 1 queue.push(curr.childNodes[i]); break; } }}
<div>asdf</div><span>active</span><div>asdf</div>
Replacing words in a body of text from array
In the most simplest form, you can do:
for(int i = 0; i < listA.Count; i++)
body = body.Replace(listA[i], listB[i]);
However, if you have a word like is
in listA
, then something like this
would be partially replaced.
UPDATE
If you want each word to be surrounded by spaces, you could add that:
for (int i = 0; i < listA.Count; i++)
{
var word1 = string.Format(@"(\b){0}(\b)", listA[i]);
var word2 = string.Format(@"$1{0}$2", listB[i]);
body = Regex.Replace(body, word1, word2, RegexOptions.IgnoreCase);
}
the regex will match the word, with spaces on either side, and replace it with the new word, keeping the spacing the same.
Replacing Text in HTML with JavaScript
Simple regular expression to fix it:
document.body.innerHTML = document.body.innerHTML.replace(/target string/g, "replacement string");
Related Topics
JavaScript At Bottom/Top of Web Page
Does Schema.Org Markup Work If Markup Is Dynamically Built With JavaScript
Canvas2D Todataurl() Different Output on Different Browser
How to Get a Style Attribute from a CSS Class by JavaScript/Jquery
How to Get the Actual Rendered Font When It's Not Defined in Css
Android Calling JavaScript Functions in Webview
How to Convert an Existing Callback API to Promises
Find Object by Id in an Array of JavaScript Objects
Upload Progress Indicators For Fetch
How to Draw an Oval in Html5 Canvas
How to Make a Jquery "$.Post" Request Synchronous
How to Update Placeholder Color Using JavaScript
How to Show Popup Message Like in Stack Overflow
React Js Conditionally Applying Class Attributes
Determine If an Element Has a CSS Class With Jquery
Detect a Finger Swipe Through JavaScript on the Iphone and Android