Javascript 如何从javascript中的字符串中提取数字
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/7033334/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
How to extract number from a string in javascript
提问by Saurabh Kumar
I have an element in javascript like follows:
我在 javascript 中有一个元素,如下所示:
<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.
我想从 span 元素中提取 280。我该怎么做?span 元素中的内容将是任何数字,后跟 ms。
回答by Alex Wayne
parseInt()
is pretty sweet.
parseInt()
很甜。
HTML
HTML
<span id="foo">280ms</span>
JS
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 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.
parseInt()
将任何字符串作为数字处理,并在遇到非数字字符时停止。在这种情况下,m
in 280ms
。在找到数字2
、8
和 之后0
,将这些数字作为基数 10(第二个参数)计算并返回数字 value 280
。请注意,这是一个实际数字而不是字符串。
Edit:
@Alex Wayne's comment.
Just filter out the non numeric characters first.
编辑:
@Alex Wayne的评论。
只需先过滤掉非数字字符。
parseInt('ms120'.replace(/[^0-9\.]/g, ''), 10);
回答by Mrchief
Try this:
尝试这个:
var num = document.getElementById('spanID').innerText.match(/\d+/)[0];
jQuery version:
jQuery 版本:
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:
如果你想作为数值(而不是字符串),使用 parseInt:
var num = parseInt($('span').text().match(/\d+/)[0], 10);
回答by JaredPar
Try the following
尝试以下
var strValue = // get 280m from the span
var intValue = parseInt(strValue.match(/[0-9]+/)[0], 10);
回答by nip3o
You could use the parseInt()
function
您可以使用该parseInt()
功能
var number = parseInt($("span").text())
回答by codecraftnap
Will it always end in "ms"? You can do:
它总是以“ms”结尾吗?你可以做:
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.
其中 s 是跨度中的字符串。要获取此值,您可以在 span 上使用 text()、html() 或 innerHTML。
回答by h0mayun
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]+/));
回答by Themer
var myNumber= $('span').text().replace(/[^d.,]+/,'');
var myNumber= $('span').text().replace(/[^d.,]+/,'');
回答by slayerIQ
Change the span in:
更改跨度:
<span id='msSpan'>280ms</span>
Then you can do:
然后你可以这样做:
alert($('#msSpan').text());
回答by Johnny Craig
using jquery is pretty simple. probably better giving the span an id though
使用 jquery 非常简单。不过最好给 span 一个 id
var mytext=replace($('span').text(),"ms","");
edited to remove ms
编辑删除ms