Get value of a string after last slash in JavaScript

前端 未结 10 1314
感情败类
感情败类 2020-11-29 19:05

I am already trying for over an hour and cant figure out the right way to do it, although it is probably pretty easy:

I have something like this : foo/bar/test

相关标签:
10条回答
  • 2020-11-29 19:15

    light weigh

    string.substring(start,end)
    

    where

    start = Required. The position where to start the extraction. First character is at index 0`.

    end = Optional. The position (up to, but not including) where to end the extraction. If omitted, it extracts the rest of the string.

        var string = "var1/var2/var3";
    
        start   = string.lastIndexOf('/');  //console.log(start); o/p:- 9
        end     = string.length;            //console.log(end);   o/p:- 14
    
        var string_before_last_slash = string.substring(0, start);
        console.log(string_before_last_slash);//o/p:- var1/var2
    
        var string_after_last_slash = string.substring(start+1, end);
        console.log(string_after_last_slash);//o/p:- var3
    

    OR

        var string_after_last_slash = string.substring(start+1);
        console.log(string_after_last_slash);//o/p:- var3
    
    0 讨论(0)
  • 2020-11-29 19:20
    var str = "foo/bar/test.html";
    var lastSlash = str.lastIndexOf("/");
    alert(str.substring(lastSlash+1));
    
    0 讨论(0)
  • 2020-11-29 19:22

    When I know the string is going to be reasonably short then I use the following one liner... (remember to escape backslashes)

    // if str is C:\windows\file system\path\picture name.jpg
    alert( str.split('\\').pop() );
    

    alert pops up with picture name.jpg

    0 讨论(0)
  • 2020-11-29 19:22
    String path ="AnyDirectory/subFolder/last.htm";
    int pos = path.lastIndexOf("/") + 1;
    
    path.substring(pos, path.length()-pos) ;
    

    Now you have the last.htm in the path string.

    0 讨论(0)
  • 2020-11-29 19:26

    You don't need jQuery, and there are a bunch of ways to do it, for example:

    var parts = myString.split('/');
    var answer = parts[parts.length - 1];
    

    Where myString contains your string.

    0 讨论(0)
  • 2020-11-29 19:27

    At least three ways:

    A regular expression:

    var result = /[^/]*$/.exec("foo/bar/test.html")[0];
    

    ...which says "grab the series of characters not containing a slash" ([^/]*) at the end of the string ($). Then it grabs the matched characters from the returned match object by indexing into it ([0]); in a match object, the first entry is the whole matched string. No need for capture groups.

    Live example

    Using lastIndexOf and substring:

    var str = "foo/bar/test.html";
    var n = str.lastIndexOf('/');
    var result = str.substring(n + 1);
    

    lastIndexOf does what it sounds like it does: It finds the index of the last occurrence of a character (well, string) in a string, returning -1 if not found. Nine times out of ten you probably want to check that return value (if (n !== -1)), but in the above since we're adding 1 to it and calling substring, we'd end up doing str.substring(0) which just returns the string.

    Using Array#split

    Sudhir and Tom Walters have this covered here and here, but just for completeness:

    var parts = "foo/bar/test.html".split("/");
    var result = parts[parts.length - 1]; // Or parts.pop();
    

    split splits up a string using the given delimiter, returning an array.

    The lastIndexOf / substring solution is probably the most efficient (although one always has to be careful saying anything about JavaScript and performance, since the engines vary so radically from each other), but unless you're doing this thousands of times in a loop, it doesn't matter and I'd strive for clarity of code.

    0 讨论(0)
提交回复
热议问题