For this specific example,

 var thenum = thestring.replace(/^\D+/g, ''); // Replace all leading non-digits with nothing

In the general case:

 thenum = "foo3bar5".match(/\d+/)[0] // "3"

Here's a bonus: regex generator.

function getre(str, num) {
  if(str === num)
    return 'nice try';
  var res = [/^\D+/g,/\D+$/g,/^\D+|\D+$/g,/\D+/g,/\D.*/g, /.*\D/g,/^\D+|\D.*$/g,/.*\D(?=\d)|\D+$/g];
  for(var i = 0; i < res.length; i++)
    if(str.replace(res[i], '') === num)
      return 'num = str.replace(/' + res[i].source + '/g, "")';
  return 'no idea';
};

function update() {
  $ = function(x) { return document.getElementById(x) };
  var re = getre($('str').value, $('num').value);
  $('re').innerHTML = 'Numex speaks: <code>' + re + '</code>';
}
<p>Hi, I'm Numex, the Number Extractor Oracle.
<p>What is your string? <input id="str" value="42abc"></p>
<p>What number do you want to extract? <input id="num" value="42"></p>
<p><button onclick="update()">Insert Coin</button></p>
<p id="re"></p>
Run code snippetEdit code snippet Hide Results Copy to answer Expand

🌐
CoreUI
coreui.io › answers › how-to-extract-numbers-from-a-string-in-javascript
How to extract numbers from a string in JavaScript · CoreUI
May 21, 2026 - For removing non-numeric characters, see how to remove special characters from a string in JavaScript. The matchAll() method with named capturing groups provides a modern, readable way to extract structured numeric data.
Discussions

Extract numbers from a string-Javascript - Code Review Stack Exchange
Consider the following string in javascript: var string="border-radius:90px 20px 30px 40px"; I want to extract the 4 numbers from that string and store them in an array called numbers.To do that I More on codereview.stackexchange.com
🌐 codereview.stackexchange.com
December 8, 2021
How to remove numbers from end of the string
howdy jishnutp, this seems to do the job ... PM(.*)\d{1,}$ it finds PM and any final digits, then grabs anything between them. it works with your data set on regex101.com if you enable "multiline". take care, lee More on reddit.com
🌐 r/regex
7
3
June 26, 2017
Extract only numbers from string ?

It will match twice because you are grouping the number with parenthesis. This should work: https://regex101.com/r/d2uvuy/3

[-+]?[0-9]+

More on reddit.com
🌐 r/regex
4
3
January 11, 2019
How to get keys from enum without converting to any?

I'm not really sure I understand, but isn't Object.keys(ShoeType) what you want?

More on reddit.com
🌐 r/typescript
9
12
August 24, 2019
🌐
GeeksforGeeks
geeksforgeeks.org › javascript › extract-a-number-from-a-string-using-javascript
Extract a Number from a String using JavaScript - GeeksforGeeks
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.
Published   July 11, 2025
🌐
EncodedNA
encodedna.com › javascript › how-to-get-numbers-from-a-string-in-javascript.htm
How to Get Numbers from a String in JavaScript
One of the simplest ways of extracting numbers from a given string in JavaScript is using Regex or Regular Expressions.
🌐
David Walsh
davidwalsh.name › javascript-extract-string
Extract a Number from a String with JavaScript
March 17, 2024 - To employ a regular expression to get a number within a string, we can use \d+: const string = "x12345david"; const [match] = string.match(/(\d+)/); match; // 12345 · Regular expressions are capable of really powerful operations within JavaScript; this practice is one of the easier operations. Converting the number using a Number() wrapper will give you the number as a Number type.
🌐
Bobby Hadz
bobbyhadz.com › blog › javascript-extract-number-from-string
How to Extract a Number from a String in JavaScript | bobbyhadz
The String.replace() method will extract a number from the string by replacing all non-digit characters with empty strings.
🌐
Sabe
sabe.io › blog › javascript-extract-numbers-from-string
How to Extract Numbers from a String in JavaScript - Sabe.io
December 29, 2022 - Now let's see how we can extract multiple numbers from a string. ... However, now let's add the g flag which makes this regular expression global, meaning it will match all numbers in the string. JAVASCRIPTconst string = "The numbers are 123 and 456"; const numbers = string.match(/\d+/g); console.log(numbers); Now you should get an array of all the numbers in the string:
Find elsewhere
🌐
TutorialsPoint
tutorialspoint.com › article › extract-a-number-from-a-string-using-javascript
Extract a number from a string using JavaScript
March 15, 2026 - In JavaScript, there are multiple ways to extract a number from a string. One way is to use the match() method and a regular expression to search for all numeric digits in the string.
🌐
TecHighness
techighness.com › home › how to extract numbers from a string in java script
How to Extract Numbers From a String in JavaScript - TecHighness
January 8, 2022 - Change the method call to extractNumbers(str, {string: false});, and replace the function code with: const extractNumbers = (text, options) => { let numbers; options = options || {}; if (!text || typeof text !== "string") { return []; } numbers ...
🌐
Linux Genie
linuxgenie.net › home › how to extract/retrieve a number from string in javascript?
How to Extract/Retrieve a Number From String in JavaScript? - Linux Genie
June 11, 2023 - For extracting numbers from a string in JavaScript, utilize the “replace()” method or “match()” method with regular expressions. You may easily extract numeric values from strings in JavaScript by using techniques such as regular ...
🌐
RSWP Themes
rswpthemes.com › home › javascript tutorial › how to extract numbers from a string in javascript
How To Extract Numbers From A String In Javascript
March 30, 2024 - Another approach to extracting numbers from a string is by leveraging JavaScript functions. This method involves iterating through each character in the string and identifying numeric characters.
🌐
Coding Beauty
codingbeautydev.com › home › posts › how to extract a number from a string in javascript
How to Extract a Number from a String in JavaScript - Coding Beauty
June 30, 2022 - If we don’t pass the global flag, ... extract a number from a string, we can also call the match() method on the string, passing a regex that matches a consecutive sequence of digits....
🌐
YouTube
youtube.com › watch
JavaScript Problem: Extracting Numbers from a String - YouTube
Occasionally you will need to extract numbers from a string. In this tutorial we show a pretty simple technique using a regular expression with the String ma...
Published   June 27, 2018
🌐
EyeHunts
tutorial.eyehunts.com › home › javascript extract number from string | regex & replace function example
JavaScript extract number from string | RegEx & Replace function example
May 16, 2021 - Simply use a regular expression in the replace function to get numbers from the string in JS. Regular expression for extracting a number is (/(\d+)/). ... Where txt is given string (with number).
🌐
YouTube
youtube.com › watch
Extract number from string JavaScript - Learn how to extract or remove numbers from strings - YouTube
In this tutorial, we’ll be looking at how to extract numbers from a string in JavaScript. This is a detailed tutorial that covers all the possible methods of...
Published   June 13, 2022
🌐
BigBinary Academy
courses.bigbinaryacademy.com › learn-javascript › number-methods › parse-int
Get Numbers From String - Learn JavaScript | BigBinary Academy
The `parseInt()` function extracts an integer from a string. - The `parseInt()` function converts each character in the string to integer, until it encounters a non-numerical value. - It returns `NaN` if the string does not start with a numerical value, `+`, `-`, or `0X`.
🌐
Techstacker
techstacker.com › how-to-extract-number-from-string-javascript
How to Extract a Number from a String With JavaScript
June 11, 2020 - The DEMO version only includes 4 pages: http://techstacker.com http://techstacker.com/embed_ext_iframe.html http://techstacker.com/topics.html http://techstacker.com/tags/html.html It is possible to download these free files and install them on your server, so you can test how the site works.
🌐
SitePoint
sitepoint.com › blog › javascript › jquery extract numbers from string
jQuery extract numbers from string — SitePoint
February 12, 2024 - To extract multiple numbers from a string using jQuery, you can use the match() method with a global regular expression. The match() method searches a string for a match against a regular expression, and returns the matches, as an array. The global regular expression (g) is used to find all ...
🌐
YouTube
youtube.com › samovar tutorials
JavaScript how to extract numbers from string - YouTube
how to extract numbers from string?Let we have a string that contains numerical values.Some values are integers.Some values have a fractional part with a dot...
Published   June 11, 2021
Views   1K