path url split() find() angular

Issue

I have urls and I want to recover the beginning of the path but I don’t know how to do it and being a beginner according to my research I should use the split() and find() method.

Here are the urls:

/test/someurls

/angular/someurls

I need to get /test and /angular is it possible to do that?

Thanks in advance.

ngOnInit() {

 let firstUrl: string = '/test/someurls';
 let secondUrl:string = '/angular/someurls';

 let t = firstUrl.split(" ").find(element =>element.startWith('/test');
    console.log(t);
}

Solution

Here is code using split function.

function getBeginning(a) {
  return "/" + a.split("/")[1];
}
console.log(getBeginning("/test/some"));
console.log(getBeginning("/angular/some"));

Answered By – Nilesh Yadav

Answer Checked By – Willingham (AngularFixing Volunteer)

Leave a Reply

Your email address will not be published.