var str = '#/promotionalMailer/test1';
输出应为==> #/promotionalMailer
我希望第二个斜杠之前的字符串'/'
到目前为止我已尝试过这个:
var str = '#/promotionalMailer/test1';
var match = str.match(/([^\/]*\/){2}/)[0];
alert(match);
但它带有第二个斜线。
答案 0 :(得分:0)
尝试http://www.mywebsite.com/?action=3
,split
和slice
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));