4 Answers4

1

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"

Joe Pitts
  • 38
  • 6
0
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.

Raj
  • 116
  • 6
0

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"
L. Monty
  • 872
  • 9
  • 17
0

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.

Ferie
  • 1,358
  • 21
  • 36