How to get number from a JavaScript string?

We can get all the numbers from JavaScript string with the replace() method.

yourVariable.replace(/[^0-9]/g,'');

Example:

<script>
    var string = 'hello 1 and 2';
    var nums = string.replace(/[^0-9]/g,'');

    console.log(nums);
</script>

Output:

12