Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to get index of the first digit in String

Tags:

I need to find out how to get the index of the first digit in String. I have an idea how to do that using some loops but it would be really ugly so I would prefer some regex. Could someone give me a clue how to do that using regex?

like image 640
Husky Avatar asked Mar 28 '13 12:03

Husky


People also ask

How do you find the index of a first digit in a string?

To get the index of the first number in a string, call the search() method passing it a regular expression that matches a digit. The search method returns the index of the first match of the regular expression in the string.

How do you get the first digit of a number Javascript?

To get the first digit of a number:Convert the number to a string. Access the string at index 0 , using square brackets notation e.g. String(num)[0] . Convert the result back to a number to get the first digit of the number.


1 Answers

firstDigit = 'Testi2ng4'.match(/\d/) // will give you the first digit in the string indexed = 'Test2ing4'.indexOf(firstDigit) 

Well I guess I need to look at my methods more closely, you can just do 'Testin323g'.search(/\d/);

like image 53
VoronoiPotato Avatar answered Sep 30 '22 18:09

VoronoiPotato