I am trying to remove
http://localhost:7001/
part from
http://localhost:7001/www.facebook.com
to get the output as
www.facebook.com
what is the regular expression that i can use to achieve this exact pattern?
I am trying to remove
http://localhost:7001/
part from
http://localhost:7001/www.facebook.com
to get the output as
www.facebook.com
what is the regular expression that i can use to achieve this exact pattern?
You don't need any library or REGEX
var url = new URL('http://localhost:7001/www.facebook.com')
console.log(url.pathname)
Based on @atiruz answer, but this is
url = url.replace( /^[a-zA-Z]{3,5}\:\/{2}[a-zA-Z0-9_.:-]+\//, '' );
To javascript you can use this code:
var URL = "http://localhost:7001/www.facebook.com";
var newURL = URL.replace (/^[a-z]{4,5}\:\/{2}[a-z]{1,}\:[0-9]{1,4}.(.*)/, '$1'); // http or https
alert (newURL);
Look at this code in action Here
Regards, Victor
This is how I made it work without resorting to regular expressions:
var URL = "http://localhost:7001/www.facebook.com";
var URLsplit = URL.split('/');
var host = URLsplit[0] + "//" + URLsplit[2] + "/";
var newURL = URL.replace(host, '');
Might not be an elegant solution though but it should be easier to understand for those who don't have much experience with regex (like me! ugh!).
For a simple regex to match any protocol, domain, and (optionally) port:
var url = 'http://localhost:7001/www.facebook.com';
// Create a regex to match protocol, domain, and host
var matchProtocolDomainHost = /^.*\/\/[^\/]+:?[0-9]?\//i;
// Replace protocol, domain and host from url, assign to `myNewUrl`
var myNewUrl = url.replace(matchProtocolDomainHost, '');
Now myNewUrl === 'www.facebook.com'
.
See demo on regex101
Regex to match the part of url, that you want to remove, will be something like: /^http[s]?:\/\/.+?\//
Example of Java code (note that in Java we use two backslashes "\\" for escaping character):
String urlWithBasePath = "http://localhost:7001/www.facebook.com";
String resultUrl = urlWithBasePath.replaceFirst("^http[s]?:\\/\\/.+?\\/", ""); // resultUrl => www.facebook.com
Example of JS code:
let urlWithBasePath = "http://localhost:7001/www.facebook.com";
let resultUrl = urlWithBasePath.replace(/^http[s]?:\/\/.+?\//, ''); // resultUrl => www.facebook.com
Example of Python code:
import re
urlWithBasePath = "http://localhost:7001/www.facebook.com"
resultUrl = re.sub(r'^http[s]?:\/\/.+?\/', '', urlWithBasePath) # resultUrl => www.facebook.com
Example or Ruby code:
urlWithBasePath = "http://localhost:7001/www.facebook.com"
resultUrl = urlWithBasePath = urlWithBasePath.sub(/^http[s]?:\/\/.+?\//, '') # resultUrl => www.facebook.com
Example of PHP code:
$urlWithBasePath = "http://localhost:7001/www.facebook.com";
$resultUrl = preg_replace('/^http[s]?:\/\/.+?\//', '', $urlWithBasePath); // resultUrl => www.facebook.com
Example of C# code (you should also specify using System.Text.RegularExpressions;
):
string urlWithBasePath = "http://localhost:7001/www.facebook.com";
string resultUrl = Regex.Replace(urlWithBasePath, @"^http[s]?:\/\/.+?\/", ""); // resultUrl => www.facebook.com
All other regular expressions here look a bit complicated? This is all that's needed: (right?)
var originSlash = /^https?:\/\/[^/]+\//i;
theUrl.replace(originSlash, '');
Alternatively, you can parse the url using as3corelib's URI class. That way you don't have to do any string manipulations, which helps to avoid making unintentional assumptions. It requires a few more lines of code, but it's a more general solution that should work for a wide variety of cases:
var url : URI = new URI("http://localhost:7001/myPath?myQuery=value#myFragment");
// example of useful properties
trace(url.scheme); // prints: http
trace(url.authority); // prints the host: localhost
trace(url.port); // prints: 7001
trace(url.path); // prints: /myPath
trace(url.query); // prints: myQuery=test
trace(url.fragment); // prints: myFragment
// build a new relative url, make sure we keep the query and fragment
var relativeURL : URI = new URI();
relativeURL.path = url.path;
relativeURL.query = url.query;
relativeURL.fragment = url.fragment;
var relativeURLString : String = relativeURL.toString();
// remove first / if any
if (relativeURLString.charAt(0) == "/") {
relativeURLString = relativeURLString.substring(1, relativeURLString.length);
}
trace(relativeURLString); // prints: myPath?myQuery=test#myFragment
instead of using regex you could just use the browser's capabilities of parsing an URL:
var parser = document.createElement('a');
parser.href = "http://localhost:7001/www.facebook.com";
var path = parser.pathname.substring(1); // --> results in 'www.facebook.com'
If you are just looking to remove the origin and get the rest of the URL, including hashes, query params and any characters without restrictions:
function getUrlFromPath(targetUrl) {
const url = new URL(targetUrl);
return targetUrl.replace(url.origin, '');
}
function main() {
const testUrls = [
'http://localhost:3000/test?search=something',
'https://www.google.co.in/search?q=hello+there+obi+wan&newwindow=1&sxsrf=ALiCzsZoaZvs0CrLQEHFmmR-MdrZ2ZHW2A%3A1665462761920&source=hp&ei=6fFEY_7cNY36wAOFyqagBA&iflsig=AJiK0e8AAAAAY0T_-R12vR7P_tmmkpEqgzmoZNczbnZA&ved=0ahUKEwi-9buirNf6AhUNPXAKHQWlCUQQ4dUDCAc&uact=5&oq=hello+there+obi+wan&gs_lcp=Cgdnd3Mtd2l6EAMyBQgAEIAEMgUIABCABDIFCAAQgAQyBQgAEIAEMgUIABCABDIFCAAQgAQyBQgAEIAEMgUIABCABDIFCAAQgAQyBQgAEIAEOgQIIxAnOhEILhCABBCxAxCDARDHARDRAzoLCAAQgAQQsQMQgwE6CwguEIAEELEDEIMBOg4ILhCABBCxAxCDARDUAjoICAAQsQMQgwE6CwguEIAEELEDENQCOggIABCABBCxAzoICC4QsQMQgwFQAFjjE2C6FmgAcAB4A4AB1QSIAd8ZkgELMC45LjIuMC4yLjGYAQCgAQE&sclient=gws-wiz'
];
testUrls.forEach(url => {
console.log(getUrlFromPath(url));
});
}
main();
A failsafe regex pattern to achieve this will get complex and cumbersome to come up with.
Just use replace
"http://localhost:7001/www.facebook.com".replace("http://localhost:7001/",'')