Remove special characters - jQuery

In this post we will see how to remove special characters like !,@,$,*,^ ...etc. from the string. For that we need to specify the special characters in regular expressions. Then by passing specified regular expression to .replace method in jquery and replace it with '' blank.

remove special characters from string in jquery

Example to remove special characters from string - jquery

<html xmlns="">
<head runat="server">
    <script type="text/javascript" src=""></script>
    <title>Remove special character fromstring</title>
    <script type="text/javascript">
        $(document).ready(function () {
            $("#Send").click(function () {
                var originalstring = $("#txtinput").val();
                var filteredString = originalstring.toLowerCase().replace( /[\*\^\'\!\@\$]/g , '');
    <form id="form1" runat="server">
    <asp:TextBox runat="server" ID="txtinput"></asp:TextBox>
    <asp:Button runat="server" ID="Send" Text="Send" />

We can use regular expressions to define the special character set that we need to remove from string. so if you want to remove * from the string the regular expression will be /[\*]/g. Now if you want to remove ! also the add ! to regular expression like /[\*\!]/g and so on.


Post a Comment