Skip to content

Instantly share code, notes, and snippets.

@wichopy
Created February 3, 2018 03:01
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save wichopy/f4eac30169f6c91497c12ee716b11ea4 to your computer and use it in GitHub Desktop.
Save wichopy/f4eac30169f6c91497c12ee716b11ea4 to your computer and use it in GitHub Desktop.
Filter out text strings using regex of a URL.
// assuming env var
// BASE_URL=www.mydomain.com
removebaseURL = (fullURL) => { // pass in (www.mydomain.com/kittens)
// Allow api calls to handle just the end point (/kittens) or the full URL (www.mydomain.com/kittens)
// by filtering out the base URL if the route contains it.
const reg = new RegExp(process.env.BASE_URL, 'gi');
const match = fullURL.match(r eg)
let endpoint
if (match) {
endpoint = fullURL.split(reg)[1]
} else {
endpoint = fullURL
}
return endpoint // returns kittens
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment