Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to get numerical value from String containing digits and characters

Tags:

javascript

Is there any built in method to get which can parse int from string ("23px")?

I know I can use substring and then parseInt but I want to know if there is any other way available to do this.

like image 610
Anoop Avatar asked Dec 22 '22 06:12

Anoop


1 Answers

parseInt will grab the first set of contiguous numbers:

parseInt('23px');

returns 23.

If there is any chance there will be leading zeros, use a radix:

parseInt('23px', 10);

which is a good habit in general.

like image 58
RobG Avatar answered Jan 03 '23 09:01

RobG