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

🌐
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
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
January 5, 2016
How to get numbers from string in Javascript? - Stack Overflow
I have a strings that can look like this: left 10 top 50 How can i extract the numbers, while the numbers can range from 0 to 100 and words can be left/right top/bottom? Thanks More on stackoverflow.com
🌐 stackoverflow.com
javascript - How to get Numbers from a String - Stack Overflow
Getting Numbers from a string and inserting them into array. splitting the string would result into single character and thus does not solve my problem. var str="(235+456+2+3-6-(2*5))" Output Mus... More on stackoverflow.com
🌐 stackoverflow.com
regex - How to find a number in a string using JavaScript? - Stack Overflow
Suppose I have a string like - "you can enter maximum 500 choices". I need to extract 500 from the string. The main problem is the String may vary like "you can enter maximum 12500 choices". So ho... More on stackoverflow.com
🌐 stackoverflow.com
May 11, 2015
🌐
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. ... David asked me if I'd be up for a guest post picking out some of my favorite Pens from CodePen.
🌐
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.
🌐
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.
🌐
TutorialsPoint
tutorialspoint.com β€Ί extract-a-number-from-a-string-using-javascript
Extract a number from a string using JavaScript
September 14, 2023 - 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.
🌐
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
🌐
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 ...
🌐
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 ...
🌐
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 - By following these methods, you can enhance your data manipulation capabilities in JavaScript. One of the most common ways to extract numbers from a string in JavaScript is by using regular expressions.
🌐
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`.
🌐
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
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
🌐
TutorialsPoint
tutorialspoint.com β€Ί article β€Ί pick-out-numbers-from-a-string-in-javascript
Pick out numbers from a string in JavaScript
March 15, 2026 - function extractNumbersWithReplace(inputString) { const numbers = []; inputString.replace(/\d+(\.\d+)?/g, function(match) { numbers.push(Number(match)); return match; }); return numbers; } const testString = "Room 101 costs $250.75 per night"; ...
🌐
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
🌐
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 ...