Get value of a string after last slash in JavaScript

前端 未结 10 1401
感情败类
感情败类 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: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);
      }
    

提交回复
热议问题