jQuery Remove Specific and Special Characters From String

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

AuthorAdmin

My name is Devendra Dode. I am a full-stack developer, entrepreneur, and owner of Tutsmake.com. I like writing tutorials and tips that can help other developers. I share tutorials of 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 from a starting stage. As well as demo example.

Leave a Reply

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