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
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
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]
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