Saurabh Kumar
Saurabh Kumar

Reputation: 16671

How to extract number from a string in javascript

I have an element in javascript like follows:

 <span>280ms</span>

I want to extract 280 from the span element. How can I do it? The content within the span element will be any number followed by ms.

Upvotes: 39

Views: 110034

Answers (10)

Ritik Mahakalkar
Ritik Mahakalkar

Reputation: 1

This extracted number from string for me:

function extractNumberFromString(inputString){

const number=[];

for(const char of inputString){

if(!isNaN(char)){

number.push(parseInt(char));

}

}

return number;

}

const i="election2025india";

console.log(extractNumberFromString(i));

//OUTPUT :- [ 2, 0, 2, 5 ]

Upvotes: 0

Alex Wayne
Alex Wayne

Reputation: 187272

parseInt() is pretty sweet.

HTML

<span id="foo">280ms</span>

JS

var text = $('#foo').text();
var number = parseInt(text, 10);
alert(number);

parseInt() will process any string as a number and stop when it reaches a non-numeric character. In this case the m in 280ms. After have found the digits 2, 8, and 0, evaluates those digits as base 10 (that second argument) and returns the number value 280. Note this is an actual number and not a string.

Edit:
@Alex Wayne's comment.
Just filter out the non numeric characters first.

parseInt('ms120'.replace(/[^0-9\.]/g, ''), 10);

Upvotes: 84

Themer
Themer

Reputation: 659

var myNumber= $('span').text().replace(/[^d.,]+/,'');

Upvotes: 0

h0mayun
h0mayun

Reputation: 3621

in general for numbers no mather negative or positive

<div>
  blah blah
  <span>285blahblah</span>
</div>

var html= document.getElementsByTagName('div')[0].innerHTML;// or $('div').html() if jquery

var number = parseFloat(html.match(/-*[0-9]+/));

http://jsfiddle.net/R55mx/

Upvotes: 0

JaredPar
JaredPar

Reputation: 755547

Try the following

var strValue = // get 280m from the span
var intValue = parseInt(strValue.match(/[0-9]+/)[0], 10);

Upvotes: 12

Mrchief
Mrchief

Reputation: 76258

Try this:

var num = document.getElementById('spanID').innerText.match(/\d+/)[0];

jQuery version:

var num = $('span').text().match(/\d+/)[0]; // or $('#spanID') to get to the span

If you want as numeric value (and not as string), use parseInt:

var num = parseInt($('span').text().match(/\d+/)[0], 10);

Upvotes: 19

codecraftnap
codecraftnap

Reputation: 1553

Will it always end in "ms"? You can do:

var num = s.substring(0, s.length-2) 

where s is the string in the span. To get this value, you can use text(), html(), or innerHTML on the span.

Upvotes: 0

nip3o
nip3o

Reputation: 3467

You could use the parseInt() function

var number = parseInt($("span").text())

Upvotes: 4

slayerIQ
slayerIQ

Reputation: 1486

Change the span in:

<span id='msSpan'>280ms</span>

Then you can do:

alert($('#msSpan').text());

Upvotes: 0

Johnny Craig
Johnny Craig

Reputation: 5000

using jquery is pretty simple. probably better giving the span an id though

var mytext=replace($('span').text(),"ms","");

edited to remove ms

Upvotes: -1

Related Questions