0

I have a url with the following query string

&center=37.793243,-122.421227

Keep in mind there are other coordinate query strings in the url. I'd simply like to grab the first pair. With the help of this post I get the following:

str.match(/[-+]?([1-8]?\d(\.\d+)?|90(\.0+)?),\s*[-+]?(180(\.0+)?|((1[0-7]\d)|([1-9]?\d))(\.\d+)?)/);

["37.793243,-122.421227", "37.793243", ".793243", undefined, "122.421227", undefined, "122", "122", undefined, ".421227"]

What would I have to do to simplify the match where it returns the 37.793243 and -122.421227 in the array?

['37.793243', '-122.421227']
Carl Edwards
  • 13,826
  • 11
  • 57
  • 119
  • 1
    Can't you take out value corresponding to `center` and split on `,` and then take out first 2 element using `array#slice`? – Hassan Imam Feb 14 '18 at 06:18
  • regex just might be overkill and overly complex here. – Mark Schultheiss Feb 14 '18 at 06:20
  • I had create a [fiddle](https://jsfiddle.net/RajeshDixit/neagdcyt/) for QS to object. You can use it and the do `obj.center.split(',')`. Just a POV but its usually difficult to manipulate a string and regex can get complicated very quickly. – Rajesh Feb 14 '18 at 06:23
  • @HassanImam What method were you thinking along the lines of extracting the `center` value? – Carl Edwards Feb 14 '18 at 06:35
  • 1
    You can use `var parsedUrl = new URL(window.location.href); parsedUrl.searchParams.get("center");` – Hassan Imam Feb 14 '18 at 06:37
  • Excellent! Feel free to post this as an answer. I'd be more than happy to mark it as correct. Thanks for the suggestion either way. – Carl Edwards Feb 14 '18 at 06:45

0 Answers0