Remove alphabet from a string in JS

Problem statement

In this, we have to verify the string that contains the special character or not and Java Script program to remove alphabets from alphanumeric string using the javascript function.

Solution

  • Create HTML file
  • Give heading as Click button to display string in<h2></h2> tag
  • Enter the string which is displayed on the output screen
  • Use button as click here to display removed character string
  • Add style by using CSS
  • Enter second heading as Verify string contains special character in <h2></h2> tag
  • Enter third heading as two string to verify that contains a special character or not in <h3></h3>tag first string is 1God2ksis345@Greate67!89  second string is MyStringContainingNoSpecialChars
  • In javascript use document.getElementById() method to return element whose id maches to specified string document.getElementById(“AB_UP”)
  • Declare the string using str variable var str = “1God2ksis345@Greate67!89”;
  • Declare the format for verify string   var format = /[ `!@#$%^&*()_+\-=\[\]{};’:”\\|,.<>\/?~]/;
  • To test the string that contains a special character or not use document.write() method verifies the string if contains it display true and if not display false document.write(format.test(“1God2ksis345@Greate67!89″) + ” 1 String Contains special characters” + “<br/>”+”<br/>”);
  • In  function stripValues()  use replace() method used to remove character from string down.innerHTML = (“Removed character string is:”) +str.replace(/[^\d.-]/g, ”);

JS code to Remove a character from string

<!DOCTYPE html>
<html>
<head>
    <meta charset="utf-8">
    <title>Verify string</title>
</head>
<body style="text-align: center;">
        <h2>
            Click button to display string
        </h2>

        <p>The string is :1God2ksis345@Greate67!89</p>
        <button id="AB_Button" onclick = "stripValues()">

            ClickHere

        </button> 


        <p id = "AB_P" style = "color:green; font-size: 20px;"></p>

        <style type="text/css">
        	.h2{
        		color: royalblue;
        		font-weight: bold;
        	}

        </style>
                 <h2>Verify string contains special charater </h2>

         <h3>String 1:1God2ksis345@Greate67!89 <br/>
         	 String 2: MyStringContainingNoSpecialChars</h3>


<script type="text/javascript">

                var up = document.getElementById("AB_UP");

            var down = document.getElementById("AB_P");

            var str = "1God2ksis345@Greate67!89";

            var format = /[ `!@#$%^&*()_+\-=\[\]{};':"\\|,.<>\/?~]/;

             document.write(format.test("1God2ksis345@Greate67!89") +  " 1 String Contains special characters" + "<br/>"+"<br/>");
             document.write(format.test("MyStringContainingNoSpecialChars")+ " 2 String Not Contains special characters" + "<br/>");


            up.innerHTML = str;

            function stripValues() {

                down.innerHTML = ("Removed character string is:") +str.replace(/[^\d.-]/g, '');

            }


</script>
</body>
</html>

Output

In this way, we learn how to verify a string that contains a special character and how to remove the character from the alphanumeric string.