var str = "7-Dec-1985"
var str = "12-Jan-1703"
var str = "18-Feb-1999"
How would I got about pulling just the year out of the string? I have tried a number of different RegExp but none seem to be working.
I would have expected re = new RegExp(/(\d+)\D*\z/);
To have worked but sadly it did not.
Any suggestions would be very appreciated
To get the last N characters of a string, call the slice method on the string, passing in -n as a parameter, e.g. str. slice(-3) returns a new string containing the last 3 characters of the original string.
substring()" is used to remove the last digit of a number.
The lastIndexOf() method returns the index (position) of the last occurrence of a specified value in a string. The lastIndexOf() method searches the string from the end to the beginning. The lastIndexOf() method returns the index from the beginning (position 0).
Strings are zero-indexed: The index of a string's first character is 0 , and the index of a string's last character is the length of the string minus 1.
this should do it
var year = str.match(/\d+$/)[0];
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