Swapnil Sawant Swapnil Sawant - 1 year ago 40
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 Source

You can try with below code.

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