Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Getting the last number in a string (JavaScript)

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

like image 406
Chris Turner Avatar asked Dec 06 '11 23:12

Chris Turner


People also ask

How do I get the last 5 characters of a string?

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.

How do you cut the last digit of a number in JavaScript?

substring()" is used to remove the last digit of a number.

How do you find the last index of a character in a string in JavaScript?

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).

What is the last character of a string in JavaScript?

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.


1 Answers

this should do it

var year = str.match(/\d+$/)[0];
like image 66
Gabriele Petrioli Avatar answered Oct 04 '22 04:10

Gabriele Petrioli