OiO.lk Community platform!

Oio.lk is an excellent forum for developers, providing a wide range of resources, discussions, and support for those in the developer community. Join oio.lk today to connect with like-minded professionals, share insights, and stay updated on the latest trends and technologies in the development field.
  You need to log in or register to access the solved answers to this problem.
  • You have reached the maximum number of guest views allowed
  • Please register below to remove this limitation

How do I replace words in an HTML page using JS?

  • Thread starter Thread starter ih8bugs
  • Start date Start date
I

ih8bugs

Guest
I want to replace all words on a webpage that follow a set condition, with other words, which vary, depending on the replaced word. For example, let's say I want to replace all words that are longer than 5 letters, with another word, also longer than 5 letters, which has the same first letter as the word that is to be replaced, and, is also longer than 5 letters. All the words on the page that are longer than 6 letters will be one of 26 possibilities.

I tried to accomplish this by taking all of the HTML and replacing all words that were longer than 5 letters and outside a tag, but my code changed the hyperlink and added my JS script to the HTML page. Why does this happen? How do I accomplish what I want to?

Code:
const segmenter = new Intl.Segmenter("en",{granularity:"word"});
const text = [...segmenter.segment(document.body.innerHTML)];
let test = "";
let alphabets = "abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ".split("");
let replacements = "assignment behind controversial desperate eventually foundation growth happiness inside jurisdiction kingdom living million nightmare original president quantum revealed surprising travels understanding vacancies weather xenophobic yourself zombie Assignment Behind Controversial Desperate Eventually Foundation Growth Happiness Inside Jurisdiction Kingdom Living Million Nightmare Original President Quantum Revealed Surprising Travels Understanding Vacancies Weather Xenophobic Yourself Zombie".split(" ");

let intag = false;
for (var word of text){
if((word.segment=="<"||word.segment==">")&&!intag){
  intag=!intag
}
  test += ((word.segment.length<6)? word.segment:replacements[alphabets.indexOf(word.segment[0])])
}
document.body.innerHTML=test;
console.log(test);

<h1> Replacing words in a webpage </h1>
<a href="https://stackoverflow.com/posts/78633403">Why won't this work?</a>

<p>I want to replace all words on a webpage that follow a set condition, with other words, which vary, depending on the replaced word. For example, let's say I want to replace all words that are longer than 5 letters, with another word, also longer than 5 letters, which has the same first letter as the word that is to be replaced, and, is also longer than 5 letters. All the words on the page that are longer than 6 letters will be one of 26 possibilities.</p>
<p>I tried to accomplish this by taking all of the HTML and replacing all words that were longer than 5 letters and outside a tag, but my code changed the hyperlink and added my JS script to the HTML page. Why does this happen? How do I accomplish what I want to?</p>
<pre><code>const segmenter = new Intl.Segmenter("en",{granularity:"word"});
const text = [...segmenter.segment(document.body.innerHTML)];
let test = "";
let alphabets = "abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ".split("");
let replacements = "assignment behind controversial desperate eventually foundation growth happiness inside jurisdiction kingdom living million nightmare original president quantum revealed surprising travels understanding vacancies weather xenophobic yourself zombie Assignment Behind Controversial Desperate Eventually Foundation Growth Happiness Inside Jurisdiction Kingdom Living Million Nightmare Original President Quantum Revealed Surprising Travels Understanding Vacancies Weather Xenophobic Yourself Zombie".split(" ");

let intag = false;
for (var word of text){
if((word.segment=="<"||word.segment==">")&&!intag){
intag=!intag
}
test += ((word.segment.length<6)? word.segment:replacements[alphabets.indexOf(word.segment[0])])
}
document.body.innerHTML=test;
console.log(test);

<h1> Replacing words in a webpage </h1>
<a href="https://stackoverflow.com/posts/78633403">Why won't this work?</a>
</code></pre>
 

Latest posts

D
Replies
0
Views
1
Dhanushka Amarakoon
D
S
Replies
0
Views
1
Shikhar Ambashta
S
Top