javascript - replace first and last occurrence of / with empty -


for /add/id/

should give add/id

i tried using replace(), replaces first occurrence. there way replace both first , last occurrence of / in single step?

you can split() string on / , slice array leaving first , last values , join on /

var str = '/add/id/';  const result = str.split('/').slice(1, -1).join('/');  console.log(result);


Comments

Popular posts from this blog

Sort a complex associative array in PHP -

vb.net - How to ignore if a cell is empty nothing -

recursion - Can every recursive algorithm be improved with dynamic programming? -