extract strings between last two slashes using regular expression in javascript

后端 未结 2 707
忘掉有多难
忘掉有多难 2021-01-23 20:25

I want to extract the string between last two string using a regular expression. I cannot seem to be able to come up with the right expression, if someone could help me that wou

2条回答
  •  走了就别回头了
    2021-01-23 20:44

    I know you said you wanted to use a regex but for those looking for a solution but don't require it to be a regex, you could do this simply by splitting the string:

     var originalString = 'aa/bbbb/ccccc/eeee/fffff.jpg';
    
     //convert to array
     var parts = originalString.split('/');
    
     //find the second to last item
     var lastPartOfPath = '';
    
     //make sure there are more than one part
     if (parts.length > 1) {
          lastPartOfPath = parts[parts.length-2];
     }
    

提交回复
热议问题