DEV Community

Cover image for Most fast function to extract numbers from string!
Matheus Maximiliano
Matheus Maximiliano

Posted on

Most fast function to extract numbers from string!

More one tutorial FAST AND FURIOUS

I don't know about you, but I have come across many times in many projects needing a function to extract numbers from some string.

And what always happens? Running to stackoverflow, but you don't need more make this.

Just save this post, and you never more need search about this function.

And the code is it:

extract numbers function

explaining the code:

  1. In the first we create a function to extract numbers that received a string with a parameter (I make a direct type in the function with if using typescript)

  2. We making a if ternary, and where have a text, this validation with regex will replace anywhere character that not be a number for a void space.

  3. Returning a result from ternary with just numbers.

.
.
.
.
.
From post in PT-BR
Instagram: @imatheus.max
Post: https://www.instagram.com/p/Cdnj7pMuFCX/

Discussion (0)