How to Remove a Word from Paragraph in JavaScript

How to Remove a Word from Paragraph in JavaScript


In this tutorial we will create a How to Remove a Word from Paragraph in JavaScript. This tutorial purpose is to teach how to remove a single word from a paragraph. This will cover all the important function that will remove a word from the paragraph. I will provide a sample program to show the actual coding of this tutorial.

This tutorial is simple and easy to understand just follow the instruction I provided and you can do it without a problem. This program can be use to any publishing system when you need to remove a certain words. I will give my best to provide you the easiest way of creating this program remove word from paragraph. So let's do the coding.

Before we get started:

This is the link for the template that i used for the layout design

Creating The Interface

This is where we will create a simple interface for our application. This code will only display form inputs and the paragraph. To create this simply copy and write it into your text editor, then save it as index.html.
  1. <!DOCTYPE html>
  2. <html lang="en">
  3. <head>
  4. <meta charset="UTF-8" name="viewport" content="width=device-width, initial-scale=1" />
  5. <link rel="stylesheet" type="text/css" href="css/bootstrap.css"/>
  6. </head>
  7. <nav class="navbar navbar-default">
  8. <div class="container-fluid">
  9. <a class="navbar-brand" href="">Sourcecodester</a>
  10. </div>
  11. </nav>
  12. <div class="col-md-3"></div>
  13. <div class="col-md-6 well">
  14. <h3 class="text-primary">How to Remove a Word from Paragraph</h3>
  15. <hr style="border-top:1px dotted #ccc;"/>
  16. <div class="col-md-4">
  17. <form>
  18. <div class="form-group">
  19. <input type="text" class="form-control" placeholder="Enter word" id="content"/>
  20. </div>
  21. <button type="button" onclick="removeWord();" class="btn btn-primary btn-block">Remove</button>
  22. </form>
  23. </div>
  24. <div class="col-md-8">
  25. <p id="result"></p>
  26. </div>
  27. </div>
  28. </body>
  29. <script src="script.js"></script>
  30. </html>

Creating JavaScript Function

This is where the main function of the application is. This code will dynamically remove the word that you search in the texbox. To do this just copy and write these block of codes inside the text editor and save it as script.js.
  1. var paragraph = "The Solar System consists of the Sun Moon and Planets. It also consists of comets, meteoroids and asteroids. The Sun is the largest member of the Solar System. In order of distance from the Sun, the planets are Mercury, Venus, Earth, Mars, Jupiter, Saturn, Uranus, Neptune and Pluto; the dwarf planet. The Sun is at the centre of the Solar System and the planets, asteroids, comets and meteoroids revolve around it.";
  3. document.getElementById('result').innerHTML = paragraph;
  6. var newParagraph = "";
  9. function removeWord(){
  10. var content = document.getElementById('content');
  11. var word = content.value;
  13. if(content.value == ""){
  14. alert("Please enter something first!");
  15. }else{
  16. if(newParagraph == ""){
  17. newParagraph = paragraph.replace(new RegExp(word, 'g'), "");
  18. }else{
  19. newParagraph = newParagraph.replace(new RegExp(word, 'g'), "");
  20. }
  22. document.getElementById('result').innerHTML = newParagraph;
  24. content.value = "";
  25. }
  26. }

In the first line of code we just simply set the paragraph into a variable so that it will be easier to call this later on. We then display the paragraph into the html page content. We created a method called removeWord(). In this method we just simple create some variables and set their corresponding function.

In order to remove a word, we just use a special JavaScript function called replace(). This function will remove a targeted string base on the condition of the regex you have set, in my case I use a condition that will only remove words in the paragraph.


The How to Remove a Word from Paragraph in JavaScript source code that I provide can be download below. Please kindly click the download button.

There you have it we successfully created How to Remove a Word from Paragraph in JavaScript. I hope that this simple tutorial help you to what you are looking for. For more updates and tutorials just kindly visit this site. Enjoy Coding!

More Tutorials for JavaScript Language

JavaScript Tutorials

Add new comment