Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How can I extract a number from a string in JavaScript?

I have a string in JavaScript (e.g #box2) and I just want the 2 from it.

I tried:

var thestring = $(this).attr('href'); var thenum = thestring.replace( /(^.+)(\w\d+\w)(.+$)/i,'$2'); alert(thenum); 

It still returns #box2 in the alert, how can I get it to work?

It needs to accommodate for any length number attached on the end.

like image 974
user1022585 Avatar asked Apr 04 '12 01:04

user1022585


People also ask

How do I strip a number from a string in JavaScript?

To remove all numbers from a string, call the replace() method, passing it a regular expression that matches all numbers as the first parameter and an empty string as the second. The replace method will return a new string that doesn't contain any numbers.

How to extract one or several numbers from a string with JavaScript?

Learn how to extract one or several numbers from a string with JavaScript. Let’s say you have a string, that includes a number, and you want to extract only the number. No problem, you can use JavaScript’s match () method.

How to match a string value with a number in JavaScript?

No problem, you can use JavaScript’s match () method. Here’s a string value, containing one number ( 1995) that is assigned to a variable called stringWithOneNumber: Now let’s attach the match () method to the variable, and add \d+ as an argument, so it looks like this match (/\d+/)

How do I extract numbers from a string in Python?

You can extract numbers from a string using a regex expression: let string = "xxfdx25y93.34xxd73"; let res = string.replace (/\D/g, ""); console.log (res); function onlyNumbers (text) { return text.replace (/\D/g, ""); }

How to extract the number 4874 from the above string?

I want to extract the number 4874 from the above string and there are two simple methods to this. The metacharacter \d search for digits, which are also numbers. The match () method uses regular expressions to retrieve it results.


1 Answers

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" 

Since this answer gained popularity for some reason, 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>
like image 57
4 revs Avatar answered Sep 28 '22 00:09

4 revs