-3

I have a str = /posts/03faf64d3-4a838-9cf474ee66ed/show

I would like to extract 03faf64d3-4a838-9cf474ee66ed and and store it into a variable how can I do that

devryan
  • 29
  • 6
  • 1
    Splitting by slashes may be wrong solution in some cases. See also this similar questions: [5642315](https://stackoverflow.com/questions/5642315) or [5874670](https://stackoverflow.com/questions/5874670). – Arthur Shlain Nov 29 '22 at 11:47

2 Answers2

2

You can use the split property when it finds a /, causing it to separate the strings into different items and this becomes an array of strings, and thus select the item you are interested in:

var str = "/posts/03faf64d3-4a838-9cf474ee66ed/show";
var result = str.split("/")[2]
Andriu1510
  • 409
  • 1
  • 6
2

Use JavaScript's split function on the string. You can split your string by /.

const str = '/posts/03faf64d3-4a838-9cf474ee66ed/show';
const parts = str.split('/');
const id = parts[2]; // contains your uuid

More on that: https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/String/split

Loránd Péter
  • 184
  • 1
  • 5