0

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!

1 Answer 1

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

// then you can use match[0]
Sign up to request clarification or add additional context in comments.

1 Comment

this worked, thanks!

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.