Javascript 使用RegExp查找url中的字符

Javascript 使用RegExp查找url中的字符,javascript,jquery,regex,Javascript,Jquery,Regex,“/产品/手表” 对于上面的字符串,我只是尝试匹配反斜杠和它们之间的字符。所以在这种情况下,我只想匹配/产品/ 使用RegExp执行此操作的基本方法是什么?或者我可以使用Jquery吗 var str = "/products/watch"; var matches = str.match(/\/(.*?)\//); if (matches) { // matches[0] is the full match including slashes "/products/" // m

“/产品/手表”

对于上面的字符串,我只是尝试匹配反斜杠和它们之间的字符。所以在这种情况下,我只想匹配/产品/

使用RegExp执行此操作的基本方法是什么?或者我可以使用Jquery吗

var str = "/products/watch";
var matches = str.match(/\/(.*?)\//);
if (matches) {
    // matches[0] is the full match including slashes "/products/"
    // matches[1] is the chars between the slashes "products"
}
工作演示: