【发布时间】:2014-12-04 10:54:35
【问题描述】:
场景
我想从 document.location 中提取路径字符串,不包括前导斜杠。 例如,如果 url 是:
http://stackoverflow.com/questions/ask
我会得到:
questions/ask
这应该很简单:
/* group everything after the leading slash */
var re = /\/(.+)/gi;
var matches = document.location.pathname.match(re);
console.log(matches[0]);
但是,如果我在 firebug 控制台中运行这个 sn-p,我仍然会得到前导斜杠。 我已经测试了regexp,正则表达式引擎正确提取了组。
问题
如何正确获取第 1 组字符串?
【问题讨论】:
标签: javascript regex regex-group