Monday, May 20, 2024
 Popular · Latest · Hot · Upcoming
31
rated 0 times [  33] [ 2]  / answers: 1 / hits: 19922  / 11 Years ago, fri, may 31, 2013, 12:00:00

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?


More From » regex

 Answers
28

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('/'));

[#77886] Thursday, May 30, 2013, 11 Years  [reply] [flag answer]
Only authorized users can answer the question. Please sign in first, or register a free account.
marissatinao

Total Points: 447
Total Questions: 90
Total Answers: 93

Location: Puerto Rico
Member since Sun, Jun 27, 2021
3 Years ago
;