-3

i am dealing with strings in vuejs. Now I have 4 url ​​strings:

https://web-sand.com/product/slug/apple-iphone-13
https://web-sand.com/product/slug/samsung-galaxy
https://web-sand.com/product/slug/xiaomi-red
https://web-sand.com/product/slug/apple-ipad

Now I want to process to get the final string. Since my string is not fixed length using fixed ways is not efficient. Result I want to get :

apple-iphone-13
samsung-galaxy
xiaomi-red
apple-ipad

Everyone please give me any comments, thanks.

Miedkes
  • 589
  • 1
  • 5
  • 16
  • Does this answer your question? [How to get the last part of a string in JavaScript?](https://stackoverflow.com/questions/6165381/how-to-get-the-last-part-of-a-string-in-javascript) – Always Helping Apr 20 '22 at 04:06
  • every answer uses `str.split('/').pop()` ... what about `str.substring(str.lastIndexOf('/')+1)` - sure, it's longer code, but at least it's not the same answer 3 times :p or `str.replace(/^.+\//, "")` – Bravo Apr 20 '22 at 04:42

2 Answers2

1

You can use:

function getStr(str) {
   return str.split('\/').pop()
}
Tanay
  • 871
  • 1
  • 6
  • 12
1

Here:

input.split("\n").map(line => line.split("/").pop())
User81646
  • 628
  • 4
  • 13