You can split the string based on any character (e.g. space) and then pass the first index to parseInt
var str = "223 lorem"; var num = parseInt(str.split(' ')[0], 10);
Demo
Note that parseInt accepts the second parameter, which is the base. If you leave this and try to analyze a number with a zero number, for example 09 , it will assume that you are in base 8 and will return 0, since 09 not a valid base 8 value.
Or, as John points out, using the unary + operator is a great way to convert a string to a number:
var str = "223 lorem"; var num = +str.split(' ')[0];
Demo
Adam rackis
source share