How to Remove Punctuation in a Phrase in JavaScript

Introduction

In this tutorial we will create a How to Remove Punctuation in a Phrase in JavaScript. This tutorial purpose is to provide you a technique to remove punctuation in a phrase. This will tackle the removing of punctuation in a phrase dynamically. I will provide a sample program to show the actual coding of this tutorial.

This tutorial is easy to understand just follow the instruction I provided and you will do it without a problem. This program is useful if you want to remove any special characters in a whole paragraph. I will give my best to give you the easiest way of creating this program How to Remove Punctuation in a Phrase. So let's do the coding.

Before we get started:

This is the link for the template that i used for the layout design https://getbootstrap.com/.

Creating The Interface

This is where we will create a simple interface for our application. This code will display an interface that will a short phrase. 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"/>
  5. <link rel="stylesheet" type="text/css" href="css/bootstrap.css" />
  6. blockquote {
  7. background: #f9f9f9;
  8. border-left: 10px solid #ccc;
  9. margin: 1.5em 10px;
  10. padding: 0.5em 10px;
  11. quotes: "\201C""\201D""\2018""\2019";
  12. }
  13. blockquote:before {
  14. color: #ccc;
  15. content: open-quote;
  16. font-size: 4em;
  17. line-height: 0.1em;
  18. margin-right: 0.25em;
  19. vertical-align: -0.4em;
  20. }
  21. blockquote p {
  22. display: inline;
  23. }
  24. </style>
  25. </head>
  26. <nav class="navbar navbar-default">
  27. <div class="container-fluid">
  28. <a class="navbar-brand" href="https://www.sourcecodester.com">Sourcecodester</a>
  29. </div>
  30. </nav>
  31. <div class="container-fluid">
  32. <div class="row">
  33. <div class="col-md-3"></div>
  34. <div class="col-md-6 well">
  35. <h3 class="text-primary">How to Remove Punctuation in a Phrase</h3>
  36. <hr style="border-top: 1px SOLID #8c8b8b;"/>
  37.  
  38. <blockquote id="text">One happy customer wrote: "This is the best program-- that I have ever seen!"</blockquote>
  39. <button onclick="rem();">Remove Punctuation</button> <button onclick="reset();">Reset</button>
  40. </div>
  41. </div>
  42. </div>
  43. </body>
  44. <script src="script.js"></script>
  45. </html>

Creating JavaScript Function

This is where the main function of the application is. This code will dynamically remove the punctuation within a phrase To do this just copy and write these block of codes inside the text editor and save it as script.js.
  1. function rem(){
  2. const text=document.getElementById("text").innerHTML;
  3.  
  4.  
  5. const remPunc = text.replace(/[.,\/#!$%"\^&\*;:{}=\-_`~()]/g, "").replace(/\s{2,}/g, " ");
  6.  
  7. document.getElementById("text").innerHTML=remPunc;
  8.  
  9. }
  10.  
  11. function reset(){
  12. window.location="index.html";
  13. }

In this code we display a simple phrase using the html tag blockqoute and bind it an id called text. We create a method to enclose multiple functions, we then get the value text id. We use the replace function to remove any characters base on the given argument, in my case we only targeted special characters with the use of the regex.

Output:

The How to Remove Punctuation in a Phrase 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 Punctuation in a Phrase 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