Basque0407 Basque0407 - 1 year ago 115
Javascript Question

Get protocol, hostname, and path from URL

I have made a Regular expression that captures the Short URL of a link. For example:

My regular expression would be:


Now this would only capture:

What I want to do now is store the captured RegEx into a variable. Any help or suggestions are greatly appreciated.

Answer Source

Your regex won't capture

Use capturing group and apply your regex with regex.exec(). Then access the returned array to set your variable:

regex = new RegExp('(https?://.*?\)/');
match = regex.exec(str)[1];

Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download