正则表达式捕获斜杠之间的字符串

时间:2016-06-25 11:59:10

标签: javascript regex

我想捕捉/ /

之间的节目名称

http://www.voot.com/shows/ splitsvilla-S09 /1/411756/love-turns-sour/425120

我试过

window.location.href.split('/')[4];

如何捕捉突出显示的部分?

4 个答案:

答案 0 :(得分:1)

试试这个:

var url = "http://www.voot.com/shows/splitsvilla-s09/1/411756/love-turns-sour/425120";
var name = /shows\/([^\/]+)/.exec(url)[1];

答案 1 :(得分:1)

试一试:

var string = "http://www.voot.com/shows/splitsvilla-s09/1/411756/love-turns-sour/425120"

var matches = string.match(/(\/[\w+-]+)/g);

console.log(matches[2].replace("/",""));

答案 2 :(得分:0)

试试这个

var str  = "http://www.voot.com/shows/splitsvilla-s09/1/411756/love-turns-sour/425120"
str.match(/shows\/\w+/g).join('').split('/')[1]

答案 3 :(得分:0)

为了完整起见,提案String#match

var url = 'http://www.voot.com/shows/splitsvilla-s09/1/411756/love-turns-sour/425120';
console.log(url.match(/shows\/([^\/]*)/)[1]);