JavaScript - Replace Special Characters

In this tutorial we will create a Replace Special Characters 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:

This is the link for the bootstrap that i used for the layout design https://getbootstrap.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 - Replace Special Characters</h3>
  15.                 <hr style="border-top:1px dotted #ccc;"/>
  16.                 <button type="button" class="btn btn-primary" onclick="RemoveChar()">Remove Special Characters</button>
  17.                 <br /><br />
  18.                 <div class="col-md-2"></div>
  19.                 <div class="col-md-8">
  20.                         <h4>Strings With Special Characters</h4>
  21.                         <div style="background-color:#fff; border:1px solid #ccc; padding:10px;">
  22.                                 <p id="display"></p>
  23.                         </div>
  24.                 </div>
  25.                 <br style="clear:both;"/>
  26.                 <br /><br />
  27.                 <div class="col-md-2"></div>
  28.                 <div class="col-md-8">
  29.                         <h4>Strings Without Special Characters</h4>
  30.                         <div style="background-color:#fff; border:1px solid #ccc; padding:10px;">
  31.                                 <p id="result"></p>
  32.                         </div>
  33.                 </div>
  34.         </div>
  35.  
  36. <script src="js/script.js"></script>
  37. </body>
  38. </html>

Creating the Script

This code contains the script of the application.This code will remove all the special characters in the string to make the strong look good. 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 = "Us#in$g r#ea$l co^nt#ent d@ur$ing d%es@ign c%an dis#$tract de#si#$g%n^ers a@nd de@sig@n rev!iew t$e%ams al%ike aw^ay fro^m th@e des!ign, an#d i%nsi$st$ing on$ alwa%ys usi$ng publ@ication r$eady co%nt^e&nt c^an b@e a re$al d%rag o^n th@e des!ign pr$oc%@ess.";
  2.        
  3. document.getElementById('display').innerHTML = text;
  4.  
  5. function RemoveChar(){
  6.        
  7.         var str = text.replace(/[&\/\\#,+()$~%@^'"!:*?<>{}]/g, '');
  8.        
  9.         document.getElementById('result').innerHTML = str;
  10.        
  11. }
There you have it we successfully created a 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