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?
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.
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.
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/)
;
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With