Get value of a string after last slash in JavaScript

前端 未结 10 1317
感情败类
感情败类 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:28

    Jquery:

    var afterDot = value.substr(value.lastIndexOf('_') + 1);
    

    Javascript:

    var myString = 'asd/f/df/xc/asd/test.jpg'
    var parts    = myString.split('/');
    var answer   = parts[parts.length - 1];
    console.log(answer);
    

    Replace '_' || '/' to your own need

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

    Try this:

    const url = "files/images/gallery/image.jpg";
    
    console.log(url.split("/").pop());

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

    As required in Question::

    var string1= "foo/bar/test.html";
      if(string1.contains("/"))
      {
          var string_parts = string1.split("/");
        var result = string_parts[string_parts.length - 1];
        console.log(result);
      }  
    

    and for question asked on url (asked for one occurence of '=' )::
    [http://stackoverflow.com/questions/24156535/how-to-split-a-string-after-a-particular-character-in-jquery][1]

    var string1= "Hello how are =you";
      if(string1.contains("="))
      {
          var string_parts = string1.split("=");
        var result = string_parts[string_parts.length - 1];
        console.log(result);
      }
    
    0 讨论(0)
  • 2020-11-29 19:36

    Try;

    var str = "foo/bar/test.html";
    var tmp = str.split("/");
    alert(tmp.pop());
    
    0 讨论(0)
提交回复
热议问题