-2

Having the following link: /C:\Users\xxx\Desktop\yyy\public\assets\1634648850202.jpg

How do i extract 1634648850202.jpg?

i tried:

const lastplace = thisUrl.substring(thisUrl.lastIndexOf("\"));

This does not work, because the backslash is recognized as a functional character (is this the right term?)

Here are 2 Questions from my side:

  1. how do i extract the last part?
  2. how should i handle backslashes in general?
nt369
  • 57
  • 1
  • 10

1 Answers1

1

Common way:

thisUrl.split(/\/|\\/).pop() // 1634648850202.jpg

I also recommend to split url/path by both slashes (/,\) 'cause of different enviromnents/systems uses diffrent character.

UPD: just make your \ double to handle it (you're right, it's a escape character), like this:

''.lastIndexOf('\\')

Also you need do the same when you initiate your string:

const thisUrl = '/C:\\Users\\xxx\\Desktop\\yyy\\public\\assets\\1634648850202.jpg';
Xeelley
  • 1,081
  • 2
  • 8
  • 18
  • @empiric sure, what's the thing I missed? – Xeelley Oct 19 '21 at 14:00
  • @empiric Er, that's because the string the OP gave didn't fully escape all the `\` characters, not because of anything contained in this answer. – esqew Oct 19 '21 at 14:04
  • @empiric sure, I had to clarify here also need to escape slashes during initialization. Thanks ! – Xeelley Oct 19 '21 at 14:06