1

I'm working on processing a URL in to it's constituent parts from my stand alone script on Google App Script.

My issue is that the regex buiding tools on the net tell me that my regex is right, but I get only one value.

Specifically, my code is:

function UrlComponents(url) {
  const _urlMatchPattern = /^((http[s]?):\/)?\/?([^:\/\s]+)(:([^\/]*))?((\/\w+)*\/)([\w\-\.]+[^#?\s]+)(\?([^#]*))?(#(.*))?$/mig;   // Source: http://stackoverflow.com/questions/27745/getting-parts-of-a-url-regex

  if (!_urlMatchPattern.test(url))
    return new Array();

  var urlMatches = url.match(_urlMatchPattern);

  if (urlMatches != null)
  {
    Logger.log("Count:" + Math.floor(urlMatches.length));

    for (var i=0; i<Math.floor(urlMatches.length);i++)
      Logger.log("Position " + i + ": " + urlMatches[i]);
  }

  return urlMatches;
}

When I test this by running the code, the output is a single array element containing the fill string. However, the same thing through a regex testing tool (e.g. http://gskinner.com/RegExr/ or www.regexplanet.com/advanced/java/) gives the correct information.

Any help / advice / well mannered pointing out of my inevitable stupid error is gratefully welcomed.

With thanks, Jonny

Jonny Mak
  • 13
  • 3

1 Answers1

1

To fix the problem you have to eliminate the /g modifier from your regular expression, i.e. the line should be

const _urlMatchPattern = /^((http[s]?):\/)?\/?([^:\/\s]+)(:([^\/]*))?((\/\w+)*\/)([\w\-\.]+[^#?\s]+)(\?([^#]*))?(#(.*))?$/mi;
megabyte1024
  • 8,482
  • 4
  • 30
  • 44
  • Thanks a lot, megabyte1024. Works like a charm. I had believed that I had tested every combination of this. – Jonny Mak Aug 10 '12 at 13:07
  • @Jonny Mak if i want extract all the links from a page without breaking url to its parts which part of your code should i use? – user1788736 Oct 21 '15 at 23:56