Skip to content

Instantly share code, notes, and snippets.

@uccmen
Last active December 31, 2015 03:29
Show Gist options
  • Save uccmen/7927626 to your computer and use it in GitHub Desktop.
Save uccmen/7927626 to your computer and use it in GitHub Desktop.
A simple JavaScript function get_integer_from_string() that will convert an input string to an integer, and return all integers found in that string WITHOUT using any built-in library functions like parseInt() and Number(). Return 0 if no numerical sequence is found.
function get_integer_from_string(str)
{
var result = 0,strint = (str.match(/\d/g)!==null)? result = str.match(/\d/g).join("")*1:result=0;
return result;
}
get_integer_from_string("100"); // returns 100
get_integer_from_string("100abc"); // returns 100
get_integer_from_string("100abc123"); // returns 100123
get_integer_from_string("1a0b0c1d2e3"); // returns 100123
get_integer_from_string("abc"); // returns 0
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment