Example: How to Remove Special Characters from a String in jQuery

Posted by

This tutorial will cover how to remove special characters from strings using jQuery. I want to show you how to get rid of special characters in a string using jQuery. You will discover how to use jquery to eliminate all special characters from a string. In-depth instructions on replacing a special character in a string using jQuery are provided in this article. Let’s get started with the steps now.

In this little example, I’ll show you how to use jQuery to remove all special characters from a string. To eliminate all special characters from text, we shall utilise the replace function. So let’s execute the HTML file below.

index.html

<!DOCTYPE html>
<html>
<head>
    <script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.6.1/jquery.min.js"></script>
</head>
<body>
  
</body>
  
<script type="text/javascript">
    $(document).ready(function () {
    
        /* Remove Special Character from String in JQuery */
        myString = "Hi, It$%#abhishek@@@";
        newString = myString.replace(/[^\w\s]/gi, '');
  
        console.log(newString);
   
    });
</script>
  
</html>

OutPut:

Hi, Itabhishek 
0 0 votes
Article Rating
Subscribe
Notify of
guest
0 Comments
Inline Feedbacks
View all comments
0
Would love your thoughts, please comment.x
()
x