Skip to content
Advertisement

Remove characters from beginning and end string

I want to ouput only MYID from URL. What I did so far:

JavaScript

output: https://whatever.expamle.com/display/MYID

JavaScript

ouput: MYID?out=1234567890?Browser=0?OS=1

How can I combine this? Thanks.

Advertisement

Answer

When the string is always a Uniform Resource Locator (URL), like you present it in your question,

given the following string:

JavaScript

you can benefit from parsing it first:

JavaScript

and then making use of the fact that MYID is the last path component:

JavaScript

and then depending on your needs, you can combine with any of the other parts, for example just the last path component with the query string:

JavaScript

Point in case is: If the string already represents structured data, use a dedicated parser to divide it (cut it in smaller pieces). It is then easier to come to the results you’re looking for.

If you’re on Linux/Unix, it is even more easy and works without a regular expression as the basename() function returns the paths’ last component then (does not work on Windows):

JavaScript
User contributions licensed under: CC BY-SA
8 People found this is helpful
Advertisement