Swapnil Sawant Swapnil Sawant - 5 months ago 13
Ruby Question

Regex to remove characters from string upto 3rd last occurance of "/"?

input

string = "/home/188/tool/public/style_photos/111111117/Detail.jpg"


expected

output = "/style_photos/111111117/Detail.jpg"


what will be the regex to do this.
Please help me.

Answer

You can try with below code.

string = string.split('/').last(3).join("/")