JS remove everything after the last occurrence of a character

前端 未结 9 1841
梦如初夏
梦如初夏 2020-12-11 16:47

Okay I have this

var URL = \"http://stackoverflow.com/questions/10767815/remove-everything-before-the-last-occurrence-of-a-character\";
console.log(URL.subst         


        
相关标签:
9条回答
  • 2020-12-11 17:39

    Try this jsfiddle or run the code snippet.

        var URL = "http://stackoverflow.com/questions/10767815/remove-everything-before-the-last-occurrence-of-a-character";
        var myRegexp = /^(.*\/)/g;
        var match = myRegexp.exec(URL);
        alert(match[1]); 

    0 讨论(0)
  • 2020-12-11 17:40

    Run this:

    var URL = "http://stackoverflow.com/questions/10767815/remove-everything-before-the-last-occurrence-of-a-character";
    var temp = URL.split('/');
    temp.pop();
    var result = temp.join('/');
    alert(result);

    0 讨论(0)
  • 2020-12-11 17:43
    console.log(URL.substring(0, URL.lastIndexOf("/")+1));
    
    0 讨论(0)
提交回复
热议问题