I have a string for example "Hello_World_1_x.txt" and I want to take what ever is after the last underscore and before the .txt. The .txt will always be there as well as the underscore but there could be many underscores. I used .split() to get rid of the last 4 characters but I want only whats after the LAST underscore. So far my regex only gives me whats after the first underscore.
You could use standard string functions:
var result = s.substring(s.lastIndexOf("_") + 1, s.lastIndexOf("."));
Try this regex:
/_([^_.]*)\.txt$/i
Using String#match:
var r = 'Hello_World_1_x.txt'.match(/_([^_.]*)\.txt$/)[1];
//=> x
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