Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Javascript regex to extract the string before the last backslash [duplicate]

I am dealing with timezone's in Javascript and I need a regex that will extract everything, but the timezone name from it. For example, I have the timezone America/Argentina/Buenos_Aires. I want to extract the America/Argentina part with a regex. Currently I have this regex: tz.match(/.*?(?=\/|$)/i)[0] which extracts everything to the first backslash which works for most timezones (America/Los_Angeles), but not for all of them. How could I edit that regex so that it gets the string before the last value?

like image 742
chromedude Avatar asked May 31 '13 18:05

chromedude


1 Answers

I'd personally suggest avoiding regular expressions for something like this, when simple string functions/methods would suffice admirably:

var stringVariable = 'America/Argentina/Buenos_Aires',
    text = stringVariable.substring(0, stringVariable.lastIndexOf('/'));
like image 76
David Thomas Avatar answered Oct 26 '22 07:10

David Thomas