There are so many ways to extract data from a string whether it be substring, split, etc
. This is an open ended question and may be too open ended, but is there a "right" way to do this? For example I have a URL http://myServer:8000/courses/courseName/courseID
what is the right way to extract out courseID
in this instance?
This is an example of what I have done:
const test2 = test.split('/courses/').slice(1).toString();
const test3 = test2.substring(0, test2.lastIndexOf('/'))
console.log(test3)
The following piece of code can help you get the last part of the URL in a single line using Javascript. This will work if the CourseID is always the last element of the URL. I am not sure how fast is it but definitely, it is elegant
url.substr(url.lastIndexOf('/') + 1)