Dada
Dada

Reputation: 65

Regex to display first set of numbers in string - Angular

I have a string in my Angular project and want to extract the first sequential set of numbers from that string.

example: aaa123aaaa95 will return 123 bbbb2bbb393 will return 2

What I've tried: I can extract all digits of my string with this code:

newVar = aaaa123aaaa6
var regex = /\d+/g;
      var matches = newVar.match(regex);  // creates array from matches

      console.log(matches)

Above code will return '1236'. Instead of all digits I just want to return the first sequence of digits '123'. Any help would be appreciated!

Upvotes: 0

Views: 509

Answers (1)

munleashed
munleashed

Reputation: 1685

const match = yourString.match(/\d+/)

// then you can use match[0]

Upvotes: 1

Related Questions