How can I extract an actual URL from a string? [duplicate] - javascript

This question already has answers here:
How do I parse a URL into hostname and path in javascript?
(26 answers)
Closed 2 years ago.
Let's say I have a string like this
let content = "https://stackoverflow.com/questions/ask";
Now I want to write a regex what will give me only the actual domain with the protocol
example: from content, I should get only "https://stackoverflow.com"

No need to write a regex when the ability to parse URLs already exist.
MDN: Web technology for developers > See Web APIs > URL
const url = new URL("https://stackoverflow.com/questions/ask");
console.log(`${url.origin}`); // Includes port
console.log(`${url.protocol}//${url.hostname}`); // Alternatively...

Related

How to get current url along with query string in jQuery [duplicate]

This question already has answers here:
Get path and query string from URL using javascript
(5 answers)
Closed 5 years ago.
I want current url path along with its query string.
I have tried below but not getting desired output.
var pathname = window.location.pathname; // Returns path only
var url = window.location.href;
Suppose my url is http://localhost:1111/Contact/Index?text=aa.
Note : query string is optional here if there is no query string then it should provide pathname only.
I want /Contact/Index?text=aa, TIA.
In jQuery you can do
$(location).attr('href')
The location object also contains properties, like host, hash, protocol, and pathname.
This window.location.pathname will give you the path or the url.

PHP : Regex to extract data from web url [duplicate]

This question already has answers here:
Parsing domain from a URL
(19 answers)
Closed 5 years ago.
I am receiving URL from an application as post data.
The URL has a signature : //abc.com/name?q=123456.
I want to extract name from the URL using regex ( Regular Expression )
How do I do that?
Best method is parsing url, but if you need regex to extract name :
/.+\/([^\?]+?)(\?|$)/gm
Demo
Note: this regex pattern match whole url, but captures name that you want in Group 1.

Remove everything after domain and http in url javascript [duplicate]

This question already has answers here:
Get current URL with jQuery?
(33 answers)
Closed 7 years ago.
I have the full url in a javascript variable and I want to strip it down to the bare url.
For example:
https://www.google.com/hello/hi.php,
http://youtube.com
strip down to:
www.google.com, youtube.com
How would I do this in javascript?
Thanks
This is not similar to the other links as I am doing this within a chrome extension, therefore the only way to get the url is using the chrome extension api which only provides the full url. As such I need to strip the full url
You can try this:
\/\/([^\/,\s]+\.[^\/,\s]+?)(?=\/|,|\s|$|\?|#)
Regex live here.
Live JavaScript sample:
var regex = /\/\/([^\/,\s]+\.[^\/,\s]+?)(?=\/|,|\s|$|\?|#)/g;
var input = "https://www.google.com/hello/hi.php, http://youtube.com,"
+ "http://test.net#jump or http://google.com?q=test";
while (match = regex.exec(input)) {
document.write(match[1] + "<br/>");
};
Hope it helps
Use the global window.location.hostname variable, and it will give you this information.

JavaScript regex for URL [duplicate]

This question already has answers here:
What is the best regular expression to check if a string is a valid URL?
(62 answers)
Closed 8 years ago.
I have to run a JavaScript regex on URLs to detect any (one or more) special character after URL and put space before them, like:
url, --> url , or url), --> url ),
e.g.
(https://www.microsoft.com), into (https://www.microsoft.com ),
var myregexp =/(\b(https?|ftp|file|http):\/\/[\-A-Za-z0-9+&##\/%?=~_|!:,.;]*[\-A-Za-z0-9+&##\/%=~_|])/g;
var str="http://www.microsoft.com)"; //url
var res=myregexp.exec(str); //execute regex query
str=str.substring(0,myregexp.lastIndex)+' '+str.substring(myregexp.lastIndex);
I used Regex query for url from : https://stackoverflow.com/a/163684

javascript url regexp restrict multiple http(s)/www [duplicate]

This question already has answers here:
Regular expression for URL
(10 answers)
Closed 8 years ago.
here is my regexp
var url_reg = /^(http[s]?:\/\/|ftp:\/\/)?(www\.)?[a-zA-Z0-9-\.]+\.(com|org|net|mil|edu|ca|in|au)+/;
it works fine for single input like https://www.google.com , but
it allows double or more "http/https/www" like below -
https://www.google.com/https://www.google.com/
url can also include folder like google.com/folder/file
i need to validate single occurrence of valid url.
Can anyone help me?
To validate a URL, you can use a regex. This is what I use. A valid URL per the URL spec. The URL you have provided, is actually a valid URL per the URL spec.
/^((((https?|ftps?|gopher|telnet|nntp):\/\/)|(mailto:|news:))(%[0-9A-Fa-f]{2}|[-()_.!~*';\/?:#&=+$,A-Za-z0-9])+)([).!';/?:,][[:blank:]])?$/
This was borrowed from OSWAP

Categories

Resources