2014-01-22 45 views
0

我正在研究一个节点应用程序,我需要一个正则表达式来匹配url模式并从url中获取信息,并提出可能的解决方案。正则表达式以匹配nodejs中的url模式

This are the url patterns: 
1) www.mysite.com/Paper/cat_CG10 
2) www.mysite.com/White-Copy-Printer-Paper/cat_DP5027 
3) www.mysite.com/pen/directory_pen? 
4) www.mysite.com/Paper-Mate-Profile-Retractable-Ballpoint-Pens-Bold-Point-Black-Dozen/product_612884 
5) www.mysite.com/22222/directory_22222?categoryId=12328 

These is what is want from the above url: 
1) name= "cat" value="CG10" 
2) name= "cat" value="DP5027" 
3) name= "directory" value ="pen" 
4) name="product" value ="612884" 
5) name="directory" value="22222" params = {categoryId : 12328} 

I want a regex which can match the url pattern and get the values like name, value and params out of the urls. 

回答

0

正则表达式下面将具有在其匹配组1个& 2所需的值

/^\/[^\/]+\/([^_]+)_([^\/_?]+).*$/ 

解释片由和平在弦/HP-ENVY-TouchSmart-m7-j010dx-173-Touc‌​h-Screen-Refurbished-Laptop/product_8000

  • ^:从开始
  • \/:匹配/
  • [^\/]+:匹配的一切,直到/HP-ENVY-TouchSmart-m7-j010dx-173-Touc‌​h-Screen-Refurbished-Laptop
  • \/:匹配/
  • ([^_]+)匹配和捕获前_product
  • _值:匹配_
  • ([^\/_?]+)火柴在_停止?,_/8000)之后捕获该值,
  • .*比赛直到结束 - 如果有什么
  • $结束

例子:

var re = /^[^\/]+\/[^\/]+\/([^_]+)_([^\/_?]+).*$/; 
var matches = re.exec('www.mysite.com/22222/directory_22222?categoryId=12328'); 
console.log(matches.splice(1)); 

输出:

["directory", "22222"] 
+0

我想你给的例子。 var match = req.url.match(/^[^\/]+\/[^\/]+\/([^_]+)_([^\/_?]+).*$/) ; \t console.log(match);我在控制台中显示null,可能的原因是什么? – Manu

+0

我应该看到不匹配的网址以及给你一个合适的答案 – Matyas

+0

这是我得到的网址“/ HP-ENVY-TouchSmart-m7-j010dx-173-Touch-Screen-Refurbished-Laptop/product_80​​00”来自req.url中的nodejs请求对象。 – Manu

1

此功能不会把戏中的url以及您提供的所需匹配项。它还会解析出无数个查询参数。

小提琴:http://jsfiddle.net/8a9nK/

function parseUrl(url) 
{ 
    var split = /^.*\/(cat|directory|product)_([^?]*)\??(.*)$/gi.exec(url); 
    var final_params = {}; 
    split[3].split('&').forEach(function(pair){ 
     var ps = pair.split('='); 
     final_params[ps[0]] = ps[1]; 
    }); 
    return { 
     name: split[1], 
     value: split[2], 
     params: final_params 
    }; 
} 

说明

^开始从字符串
.*匹配任意数量的任何东西(URL的,我们不关心开头)
的开始\/匹配单个反斜杠(最后一个我们关心的事物)
(cat|directory|product)垫子ch和捕捉字猫或目录或产品(这是我们
_匹配下划线(字符分隔我们
([^?]*)匹配和捕捉任何数量的任何东西,除了一个问题标记(这是我们
\??匹配一个问号,如果它存在,否则不用担心它(潜在的查询字符串的开始)
(.*)匹配和捕捉任何数量的任何东西(这是整个查询字符串,我们无线会分裂成PARAM后)
$匹配字符串的结尾

+0

你能解释这部分的结果吗“/^.*\/ (猫|目录|产品)_([^?] *)\ ??(。*)$/gi“ – Manu

+0

是的,解决方案已被修改添加一个解释。 – colbydauph

+0

如果isPartial存在如何获取该查询参数www.mysite.com/22222/directory_22222?categoryId=12328&isPartial=true – Manu

0

使用url模块来帮助你,而不是一切都需要用正则表达式:)

var uri = require('url').parse('www.mysite.com/22222/directory_22222?categoryId=12328', true); 

这将产生(做与其他的东西):

{ 
    query: { categoryId: '12328' }, 
    pathname: 'www.mysite.com/22222/directory_22222' 
} 

现在让你的最后一部分:

uri.pathParams = {}; 
uri.pathname.split('/').pop().split('_').forEach(function(val, ix, all){ 
    (ix&1) && (uri.pathParams[ all[ix-1] ] = val); 
}); 

这将产生:

{ 
    query: { categoryId: '12328' }, 
    pathParams: { directory: '22222 }, 

    ... a bunch of other stuff you don't seem to care about 
}