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

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.

Related

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

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...

In JavaScript, how do I get the full path including data after the trailing pound? [duplicate]

This question already has an answer here:
Fetching the window.location.pathname with a Pound Sign in the URL - javascript
(1 answer)
Closed 3 years ago.
My full URL is
/settings/logs#/extra
but window.location.pathname just returns
/settings/logs
In the browser, how do I get the full path including the # and the information after it?
You can use window.location.href to obtain the full url (with protocol, host, etc.) or concat the window.location.pathname with window.location.hash if you only need the "path".
console.log(window.location.href); // Result: https://www.google.com/path/#hash
console.log(window.location.pathname + window.location.hash); // Result: /path/#hash
See Location API
Try window.location.href
For further info:
https://www.w3docs.com/snippets/javascript/how-to-get-current-url-in-javascript.html

Complicated JavaScript string removal/replacement [duplicate]

This question already has answers here:
How can I delete a query string parameter in JavaScript?
(27 answers)
Closed 7 years ago.
I have a query string that I need to remove a certain parameter from. For instance, my query string may be "?name=John&page=12&mfgid=320", and I need to remove the "page" parameter from it and end up with "?name=John&mfgid=320". I cannot assume that the "page" parameter is or isn't followed by other parameters.
All my attempts at using JavaScript functions/regex are failing miserably, so I could really use a hand in getting this working. Thanks.
That's quite easy... It's just /page=\d+&?/
var uri = '?name=John&page=12&mfgid=320';
uri = uri.replace(/page=\d+&?/,'');
You can use:
uri = uri.replace(/[?&]page=[^&\n]+$|([&?])page=[^&\n]+&/g, '$1');
RegEx Demo
We'll need to use alternation to cover all the cases of presence of query parameter. Check my demo for all test cases.

How to check string in current URL with javascript? [duplicate]

This question already has answers here:
Get current URL with jQuery?
(33 answers)
Closed 8 years ago.
My Url looks like:
http://mywebsite/series/2545-abc
But I don't know how to check URL with regex. So, How to check url current windows with above URL?
I mean:
If Url = http://mywebsite/series/2545-abc
do something
What about this? The regular expression defined at the first line is tested against the URL:
var myRe = /\/series\//; //regular expression to use
if(myRe.test(window.location.href)){
alert("matching");
}else{
alert("not matching");
}
If you want to test for the whole URL (and really want to use a regular expression for that), you could replace the first line with
var myRe = /^http:\/\/mywebsite\/series\/2545-abc$/;
If you remove the dollar sign at the end, modifications at the end of the URL are accepted as well (e.g. http://mywebsite/series/2545-abc/foo.html)

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