jQuery Remove Specific and Special Characters From String

To remove specific and special characters from string jQuery; In this tutorial, you will learn how to remove specific and special characters from string in jQuery.

How to Remove Specific and Special Characters From String in jQuery

Let’s use the following methods to remove specific and special character from string in jQuery:

  • Method 1 – Remove Specific Characters From String in jQuery
  • Method 2 – jQuery remove special characters from string

Method 1 – Remove Specific Characters From String in jQuery

If you have a string “-my name is tutsmake”. And want to remove ‘-‘ character from the string; See the following example for that:

<html lang="en">
<head>
    <title>How to remove all spaces from string in JQuery? - Tutsmake.com</title>
    <script src="https://code.jquery.com/jquery-3.6.0.min.js" integrity="sha256-/xUj+3OJU5yExlq6GSYGSHk7tPXikynS7ogEvDej/m4=" crossorigin="anonymous"></script>
    <link rel="stylesheet" href="https://maxcdn.bootstrapcdn.com/bootstrap/4.5.2/css/bootstrap.min.css">
    <style>
        body{
            background: rgb(216, 232, 215);
        }
        .container{
            background: rgb(247, 228, 220);
            margin-top: 14%;
            height: 300px;
            width: 51%;
            border: 3px solid red !important;
        }
        .container h4{
            margin-bottom: 42px;
            font-size: 25px;
        }
    </style>
</head>
<body>
    <div class="container text-center border">
        <h4 class="mt-5 font-weight-bold">How to remove all spaces from string in JQuery? - tutsmake.com</h4>
        <input id="my-id" value="-my name is tutsmake">
        <button class="btn btn-primary mx-auto d-block mt-4 ">Remove White Space</button>
    </div>
    <script type="text/javascript">
        $("button").click(function(){
            var myText = $("#my-id").val();
            myText = myText.replace('-', '');
            alert(myText);
        });
    </script>
</body>
</html>

Method 2 – jQuery remove special characters from string

If you have a special character string “-my name is @#tutsmake”. And want to remove ‘@#’ character from the string; See the following example for that:

<html lang="en">
<head>
    <title>How to remove all spaces from string in JQuery? - Tutsmake.com</title>
    <script src="https://code.jquery.com/jquery-3.6.0.min.js" integrity="sha256-/xUj+3OJU5yExlq6GSYGSHk7tPXikynS7ogEvDej/m4=" crossorigin="anonymous"></script>
    <link rel="stylesheet" href="https://maxcdn.bootstrapcdn.com/bootstrap/4.5.2/css/bootstrap.min.css">
    <style>
        body{
            background: rgb(216, 232, 215);
        }
        .container{
            background: rgb(247, 228, 220);
            margin-top: 14%;
            height: 300px;
            width: 51%;
            border: 3px solid red !important;
        }
        .container h4{
            margin-bottom: 42px;
            font-size: 25px;
        }
    </style>
</head>
<body>
    <div class="container text-center border">
        <h4 class="mt-5 font-weight-bold">How to remove all spaces from string in JQuery? - tutsmake.com</h4>
        <input id="my-id" value="-my name is @#tutsmake">
        <button class="btn btn-primary mx-auto d-block mt-4 ">Remove White Space</button>
    </div>
    <script type="text/javascript">
        $("button").click(function(){
            var myText = $("#my-id").val();
            str=  myText.replace(/[^\w\s]/gi, '');
            alert(str);
        });
    </script>
</body>
</html>

Recommended jQuery Tutorials

Recommended jQuery Tutorials

AuthorDevendra Dode

Greetings, I'm Devendra Dode, a full-stack developer, entrepreneur, and the proud owner of Tutsmake.com. My passion lies in crafting informative tutorials and offering valuable tips to assist fellow developers on their coding journey. Within my content, I cover a spectrum of technologies, including PHP, Python, JavaScript, jQuery, Laravel, Livewire, CodeIgniter, Node.js, Express.js, Vue.js, Angular.js, React.js, MySQL, MongoDB, REST APIs, Windows, XAMPP, Linux, Ubuntu, Amazon AWS, Composer, SEO, WordPress, SSL, and Bootstrap. Whether you're starting out or looking for advanced examples, I provide step-by-step guides and practical demonstrations to make your learning experience seamless. Let's explore the diverse realms of coding together.

Leave a Reply

Your email address will not be published. Required fields are marked *