Getting numeric value from a alpha numeric string in javascript - Stack Overflow

I have a string "RowNumber5", now i want to get the numeric value "5" from that str

I have a string "RowNumber5", now i want to get the numeric value "5" from that string using Javascript.

Note: Numeric value will be always at the end, after alphabets, that means numeric value will never occur in between alphabets. Example -

 Result45 - Yes
 Result45Abc - Never

I can get this "5" by some thing like this

var t = "Ruby12";
var y = parseInt(t.split('').reverse().join(""));
if(!isNaN(y)) {
    y = y.toString().split('').reverse().join("");
}
else {
    y = "";
}
console.log(y);

Any shot way? or Better approach for this ?

I have a string "RowNumber5", now i want to get the numeric value "5" from that string using Javascript.

Note: Numeric value will be always at the end, after alphabets, that means numeric value will never occur in between alphabets. Example -

 Result45 - Yes
 Result45Abc - Never

I can get this "5" by some thing like this

var t = "Ruby12";
var y = parseInt(t.split('').reverse().join(""));
if(!isNaN(y)) {
    y = y.toString().split('').reverse().join("");
}
else {
    y = "";
}
console.log(y);

Any shot way? or Better approach for this ?

Share Improve this question asked Aug 5, 2013 at 12:28 Ashis KumarAshis Kumar 6,5542 gold badges23 silver badges36 bronze badges 5
  • This won't work? /.*([0-9]+)$/ – MightyPork Commented Aug 5, 2013 at 12:29
  • do you always know the alpha part of that string? if so, you can replace that substring with '' and parseInt the rest – Kuro Commented Aug 5, 2013 at 12:30
  • @Kuro: No the alpha part is dynamic. – Ashis Kumar Commented Aug 5, 2013 at 12:42
  • @MightyPork: Can you show the full implementation. – Ashis Kumar Commented Aug 5, 2013 at 12:43
  • I did now, check the answer – MightyPork Commented Aug 5, 2013 at 12:51
Add a ment  | 

4 Answers 4

Reset to default 4

This is what regexes were made for!

var matches = /\d+$/.exec("Ruby12");
matches[0];  //returns 12

var matches = /\d+$/.exec("sfwfewcsd098");
matches[0];  //returns 098

var matches = /\d+$/.exec("abc"); //matches returns null

Here's a regex solution:

// input string
var t = "Ruby12";

var num = null;    
var match = t.match(/([0-9]+)$/);    
if(match!=null) num = parseInt(match[1]);

// num now contains null or number

// debug
console.log(num);

Try using this regular expression

var s = "gdgdfg45";
var matches = s.match(/\d+$/);
console.log(matches[0]);

I would go with the divide and conquer method, working through all strings that may contain numbers:

var t = "Ruby12";
function get_numbers(str) {
    var parts = str.split(""), nums = "";
    for(var i = 0, len = parts.length; i < len; i++) {
        nums += (isNaN(parts[i])) ? "" : parts[i].toString();
    }
    return(nums);
}

var numbers = get_numbers(t);
alert(numbers);

Which would give you "12".

发布者:admin,转转请注明出处:http://www.yc00.com/questions/1744655232a4586141.html

相关推荐

发表回复

评论列表(0条)

  • 暂无评论

联系我们

400-800-8888

在线咨询: QQ交谈

邮件:admin@example.com

工作时间:周一至周五,9:30-18:30,节假日休息

关注微信
['keyword'] : $thread['subject']; $header['description'] = $thread['description'] ? $thread['description'] : $thread['brief']; $_SESSION['fid'] = $fid; if ($ajax) { empty($conf['api_on']) and message(0, lang('closed')); $apilist['header'] = $header; $apilist['extra'] = $extra; $apilist['access'] = $access; $apilist['thread'] = well_thread_safe_info($thread); $apilist['thread_data'] = $data; $apilist['forum'] = $forum; $apilist['imagelist'] = $imagelist; $apilist['filelist'] = $thread['filelist']; $apilist['threadlist'] = $threadlist; message(0, $apilist); } else { include _include(theme_load('single_page', $fid)); } break; default: message(-1, lang('data_malformation')); break; } ?>