Skip to content

Instantly share code, notes, and snippets.

@lusan
Created September 14, 2017 11:17
Show Gist options
  • Save lusan/5aca36f0b9eafe61e04ff173010bb783 to your computer and use it in GitHub Desktop.
Save lusan/5aca36f0b9eafe61e04ff173010bb783 to your computer and use it in GitHub Desktop.
export const removeBaseUrl = (url) => {
/*
* Replace base URL in given string, if it exists, and return the result.
*
* e.g. "http://localhost:8000/api/v1/blah/" becomes "/api/v1/blah/"
* "/api/v1/blah/" stays "/api/v1/blah/"
*/
const baseUrlPattern = /^https?:\/\/[a-z\:0-9.]+/;
let result = "";
let match = baseUrlPattern.exec(url);
if (match != null) {
result = match[0];
}
if (result.length > 0) {
url = url.replace(result, "");
}
return url;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment