example:https://www.google.com/upload/images/2021-5-3/3fde799e-1b53-33aa-b6b4-181d00a26a1f/output-001.svg
I want to get only "upload/images"
example:https://www.google.com/upload/images/2021-5-3/3fde799e-1b53-33aa-b6b4-181d00a26a1f/output-001.svg
I want to get only "upload/images"
By using the URL class you can get the pathname.
let url = new URL("https://www.google.com/upload/images/2021-5-3/3fde799e-1b53-33aa-b6b4-181d00a26a1f/output-001.svg");
$ url.pathname -> "/upload/images/2021-5-3/3fde799e-1b53-33aa-b6b4-181d00a26a1f/output-001.svg"
let url = window.location.href;
let pathname = url.split('.com/')[1]
let param = pathname.split('/');
let result = `${param[0]}/${param[1]}`
This is a bit lengthy but gives you required result.
You could use a regular expression to match everything between 3rd and 5th slash, e.g.:
let url = "https://www.google.com/upload/images/2021-5-3/3fde799e-1b53-33aa-b6b4-181d00a26a1f/output-001.svg";
let result = url.replace(/[^/]*\/[^/]*\/[^/]*\/([^/]*\/[^/]*).*/, "$1");
which results in
"upload/images"
This is the simple way to get the route (the URL without the domain):
import { Component } from '@angular/core';
import { Router } from '@angular/router';
@Component({
template: 'Play with the routes'
})
export class MyComponent {
constructor(private router: Router) {}
ngOnInit() {
console.log(this.router.url);
}
}
From this point onward is pretty easy to play with the route and get the pieces you want: for example, split the string when you find the /
and join only the pieces that you want.
You can also think of use UrlSegment
(documentation here).
You can also have a look at this other question here.