在javascript中剪切字符串

在javascript中剪切字符串,javascript,jquery,Javascript,Jquery,将字符串从最后一个“/”剪切到“.html” 我有一根这样的绳子 "/Views/GS/stockView.html" 我需要名字“stockView” 如何从字符串中删除名称? 谢谢 使用和方法,如 var str = "/Views/GS/stockView.html"; var slashPos = str.lastIndexOf('/'); var dotPos = str.indexOf('.', slashPos + 1); var result = str.substring(s

将字符串从最后一个“/”剪切到“.html” 我有一根这样的绳子

"/Views/GS/stockView.html"
我需要名字“stockView” 如何从字符串中删除名称? 谢谢

使用和方法,如

var str = "/Views/GS/stockView.html";
var slashPos = str.lastIndexOf('/');
var dotPos = str.indexOf('.', slashPos + 1);
var result = str.substring(slashPos + 1, dotPos);
试试这个

var msg = "/Views/GS/stockView.html";
var startIndex = -1;
var endIndex=-1;
var length = msg.length;
for (var i = length - 1; i >= 0; i--) {
  if (msg[i] == '/'){
       startIndex=i+1;
       break;
  }
  if(msg[i]==".")
    endIndex=i;
}

console.log(msg.substr(startIndex,endIndex-startIndex));  
或者试试这个

var msg = "/Views/GS/stockView.html";
var split=msg.split("/");
split=split[split.length-1].split(".");
console.log(split[0]);
尝试使用RegExp:

var视图=函数(str){
返回str.match(/\/(\w*)\./)[1];//查找介于`/`和`之间的单词`
};
log(视图(“/Views/GS/stockView.html”);
log(视图(“/Views/fs/inventView.html”);
log(视图(“/Views/fs/p1/showView.jsp”);
log(视图(“/Views/fs/p2/showView123.aspx”)
打开控制台
var msg = "/Views/GS/stockView.html";
var split=msg.split("/");
split=split[split.length-1].split(".");
console.log(split[0]);