JavaScript - Remove Character From String

In this tutorial we will create a Remove Character From String using Javascript. JavaScript is a scripting or programming language that allows you to implement complex things on web pages. It is a text-based programming language meant to run as part of a web-based application. It is an interpreted programming language that has a capabilities of Object-Oriented. So Let's do the coding...

Getting Started:

First you have to download & install XAMPP or any local server that run PHP scripts. Here's the link for XAMPP server https://www.apachefriends.org/index.html. And this is the link for the jquery that i used in this tutorial https://jquery.com/.

The Main Interface

This code contains the interface of the application. To create this just write these block of code inside the text editor and save this 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="https://sourcecodester.com">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">Javascript - Remove Character From String</h3>
  15.                 <hr style="border-top:1px dotted #ccc;"/>
  16.                 <form>
  17.                         <div class="form-group">
  18.                                 <label>Remove Character</label>
  19.                                 <input type="text" id="content"/>
  20.                                 <button type="button" id="remove" class="btn btn-primary">Remove</button>
  21.                         </div>
  22.                 </form>
  23.                 <p id="result"></p>
  24.         </div>
  25. </body>
  26. <script src="js/script.js"></script>
  27. </html>

Creating the Script

This code contains the script of the application. This code will remove a specific character inside a string when user input the targeted character and pressed the button. To do this just copy and write these block of codes as shown below inside the text editor and save it as script.js inside the js folder.
  1. var text = "For. Open. Multiply itself gathered saying days thing. After. Saying divide rule rule whose may may winged make beast created. Give lights god from. Lesser fifth. Land greater bearing his moving, together seasons beginning deep abundantly deep abundantly green dominion firmament.";
  2.        
  3. document.getElementById('result').innerHTML = text;
  4.  
  5. document.getElementById('remove').onclick = removeChar;
  6.  
  7. var newText = "";
  8.  
  9.  
  10. function removeChar(){
  11.         var content = document.getElementById('content');
  12.         var target = content.value;
  13.  
  14.         if(content.value == ""){
  15.                 alert("Please enter something first!");
  16.         }else{
  17.                 if(newText == ""){
  18.                         newText = text.replace(new RegExp(target, 'g'), "");
  19.                 }else{
  20.                         newText = newText.replace(new RegExp(target, 'g'), "");
  21.                 }
  22.                
  23.                 document.getElementById('result').innerHTML = newText;
  24.                
  25.                 content.value = "";
  26.         }
  27. }
There you have it we successfully created a Remove Character From String using 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!

Add new comment