Extract a number from a string using JavaScript

Extract a number from a string using JavaScript

Improve Article

Save Article

Improve Article

Save Article

The number from a string in javascript can be extracted into an array of numbers by using the match method. This function takes a regular expression as an argument and extracts the number from the string. Regular expression for extracting a number is (/(\d+)/). 

Example 1: This example uses match() function to extract numbers from strings. 

html

<div align="center" style="background-color: green;">

    <h1>GeeksforGeeks</h1>

    <h3>Extract number from string</h3>

    <p>String is "jhkj7682834"</p>

    <p id="GFG">

        Click the button to extract number

    </p>

    <input type="button" value="click " onclick="myGeeks()">

</div>

<script>

    function myGeeks() {

        var str = "jhkj7682834";

        var matches = str.match(/(\d+)/);

         

        if (matches) {

            document.getElementById('GFG').innerHTML

                    = matches[0];

        }

    }

</script>

Output:

Extract a number from a string using JavaScript

Extract a number from a string 

Example 2: This example uses the match() function to extract numbers from strings. 

html

<body>

    <div align="center" style="background-color: green;">

        <h1>GeeksforGeeks</h1>

        <h3>Extract number from string</h3>

        <p>String is "foo35bar5jhkj88"</p>

        <h3>

            The string contains 3 numbers. So the numbers

            are stored in an array and print together

        </h3>

        <p id="GFG">

            Click the button to extract number

        </p><br>

        <h3 id="Geeks"></h3>

        <input type="button" value="Click Here!" onclick="myGeeks()">

    </div>

    <script>

        function myGeeks() {

            var str = "foo35bar5jhkj88";

            matches = str.match(/\d+/g);

            var i=0

         

            document.getElementById('GFG').innerHTML

                = matches[0] + matches[1] + matches[2];

             

            document.getElementById("Geeks").innerHTML

                = "Where 35 is the first, 5 is the second"

                + " and 88 is the third number"

            }

    </script>

</body>

Output:

Extract a number from a string

Extract a number from a string 

Example 3: This example uses reduce method to extract all the numbers from the string.

HTML

<body>

    <div align="center" style="background-color: green;">

        <h1>GeeksforGeeks</h1>

        <h3>

        </h3>

        <p>String is "jhkj7682834"</p>

        <p id="GFG">

            Click the button to extract number

        </p>

        <input type="button" value="click " onclick="myGeeks()">

    </div>

    <script>

        function myGeeks() {

            var str = "jhkj7682834";

            var c = '0123456789'

             

            function check(x) {

                        return c.includes(x)  ? true : false;

                    }

            var matches = [...str].reduce((x, y) => check(y) ? x+y : x, '')

             

            if (matches) {

                document.getElementById('GFG').innerHTML

                        = matches;

            }

        }

    </script>

</body>

Output:

Extract a number from a string

Extract a number from a string 

JavaScript is best known for web page development but it is also used in a variety of non-browser environments. You
can learn JavaScript from the ground up by following this JavaScript Tutorial and JavaScript Examples.