If I have this string
str = "22px";
How do I extract just the number 22? Thanks.
If I have this string
str = "22px";
How do I extract just the number 22? Thanks.
"22px".substring(0,2); // returns "22"
But this only works if the number has always 2 digits.
If you want an actual number, you should try parseInt(). It will take care of taking off the 'px' for you.
str = "22px";
num = parseInt(str, 10); // pass in the radix for safety :)
assert(num === 22);