Navigate back to the homepage

how to extract numbers from the string in typescript?

Infinitbility
TypeScript
July 24th, 2022 · 1 min read
how to extract numbers from the string in typescript?

Extract number from string in typescript

To extract a number from a string in typescript, use the match() method with the /\d+/g regex it will find all numbers from the string and return as an array of numbers.

1const REGEX = /\d+/g;
2
3let sampleString: string = "#div-name-1234-characteristic:561613213213";
4
5let arrOfNumber: Array<number> = sampleString.match(REGEX);
6
7// 👇️ [ '1234', '561613213213' ]
8console.log(arrOfNumber)

Today, I’m going to show you How to i extract a number from a string in typescript, as mentioned above, I’m going to use the use match() and join() method with /\d+/g regex to extract numbers from string and convert in digits.

Let’s start today’s tutorial how do you extract numbers from the string in typescript?

In this example, we will do

  1. extract number from string
  2. got extracted array of numbers and convert it a single string
  3. convert string to digits

utils.ts

1const REGEX = /\d+/g;
2
3let sampleString: string = "#div-name-1234-characteristic:561613213213";
4
5let arrOfNumber: Array<number> = sampleString.match(REGEX);
6
7// 👇️ [ '1234', '561613213213' ]
8console.log(arrOfNumber)
9
10let numStr: string = arrOfNumber.join('');
11
12// 👇️ '1234561613213213'
13console.log(numStr)
14
15let num: number = +numStr;
16
17// 👇️ 1234561613213213
18console.log(num)

In the above code example, we will do all steps separately but if you want to sort the version of the code look following code.

1const REGEX = /\d+/g;
2
3let sampleString: string = "#div-name-1234-characteristic:561613213213";
4
5let num: number = +sampleString.match(REGEX).join('');
6// 👇️ 1234561613213213
7console.log(num)

I hope it helps you, All the best 👍.

Follow me on Twitter

Join our email list and get notified about new content

No worries, I respect your privacy and I will never abuse your email.

Every week, on Tuesday, you will receive a list of free tutorials I made during the week (I write one every day) and news on other training products I create.

Looking For React Native Tutorial?

we are trying to create the best Tutorial for react native developers.

When you want a daily updates about React Native Tutorial or infinitbility update subscribe to our newsletter.

Read React Native Tutorial

Request New Tutorial or Article on mail [email protected]

Guide book

Guidebook: The Javascript Monorepo Photoshop Guide

Tutorials

Categories

More articles from Infinitbility

how to remove space between strings in javascript?

how to remove space between strings in javascript?

An example of removing space between strings in javascript with step by step guide.

July 23rd, 2022 · 1 min read
how to add character in string in javascript?

how to add character in string in javascript?

An example of adding character in string in javascript with step by step guide.

July 22nd, 2022 · 1 min read
© 2020–2022 Infinitbility
About
Link to $https://medium.com/infinitbilityLink to $https://www.facebook.com/InfinitbilityLink to $https://github.com/infinitbilityLink to $https://twitter.com/infinitbilityLink to $https://www.buymeacoffee.com/infinitbilityLink to $mailto:[email protected]