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

1 Answer 1

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;
Sign up to request clarification or add additional context in comments.

2 Comments

Thanks a lot, megabyte1024. Works like a charm. I had believed that I had tested every combination of this.
@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?

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.