Javascript - 从字符串中提取数字

时间:2012-11-23 10:20:19

标签: javascript

  

可能重复:
  How might I extract the number from a number + unit of measure string using JavaScript?

如何在JS中从字符串中提取数字。 字符串:“Some_text_123_text” - > 123

3 个答案:

答案 0 :(得分:5)

JSFiddle Demo

var s = "Some_text_123_text";
var index = s.match(/\d+/);
document.writeln(index);​

答案 1 :(得分:1)

试试这个

var string = "Some_text_123_text";
var find = string.split("_");
for(var i = 0; i < find.length ; i ++){
 if(!isNaN(Number(find[i]))){
  var num = find[i];
 }
}

alert(num);

答案 2 :(得分:0)

尝试此工作fiddle

var str = "Some_text_123_text";
var patt1 = /[0-9]/g;
var arr= str.match(patt1);
var myval = arr.join("");