0

I have a url as below

localhost:1340/promotionDetails/pwd1/pwd2?promotion_id=PROM008765

I used url module for parsing the url for the pathname below is the code

var url=require('url').parse('http://localhost:1340/promotionDetails/pwd1/pwd2?  promotion_id=PROM008765', true).pathname
console.log(url);

The output that I got is

/promotionDetails/pwd1/pwd2

I used the split function to get the pwd1 and pwd2 from the path.I want to know if there is anyother way to get pwd1 and pwd2 without using the split function.Any help will be really helpful.

4

2 に答える 2

1

分割を使用せずに正規表現を使用して、url ディレクトリを取得できます。

var myurl = "localhost:1340/promotionDetails/pwd1/pwd2?promotion_id=PROM008765";
var match = myurl.match(/[^/?]*[^/?]/g);
/* matches everything between / or ?
[ 'localhost:1340',
  'promotionDetails',
  'pwd1',
  'pwd2',
  'promotion_id=PROM008765' ]
*/
console.log(match[2]);//pwd1
console.log(match[3]);//pwd2
于 2013-03-11T18:58:54.707 に答える