Javascript正则表达式在第二个之前得到字符串/

时间:2016-02-22 09:40:38

标签: javascript regex

var str = '#/promotionalMailer/test1';

输出应为==> #/promotionalMailer

我希望第二个斜杠之前的字符串'/'

到目前为止我已尝试过这个:

 var str = '#/promotionalMailer/test1'; 
 var match = str.match(/([^\/]*\/){2}/)[0]; 
 alert(match); 

但它带有第二个斜线。

2 个答案:

答案 0 :(得分:0)

尝试http://www.mywebsite.com/?action=3splitslice

join

答案 1 :(得分:0)

例如,

var str = '#/promotionalMailer/test1/foo/bar/baz';

result = str.split('/').slice(0, 2).join('/')

document.write('<pre>'+JSON.stringify(result,0,3));

如果你想要正则表达式,那么

var str = '#/promotionalMailer/test1/foo/bar/baz';

result = str.match(/[^\/]*\/[^\/]*/)[0]

document.write('<pre>'+JSON.stringify(result,0,3));