iPhone Developer iPhone Developer - 2 months ago 6
Javascript Question

capture first part of the url

Let's say I've following URLs.

/product
/product/1
/product/1/buy

/customer
/customer/1
/customer1/contact


I'm trying for a regular expression to get the following match so I can run a switch statement on it.

/product

/customer


I've tried the following and trying other options as well.

request.url.match(/^\/(.*)(\/?)/)

Answer

arr = [
        '/product',
        '/product/1',
        '/product/1/buy',
        '/customer',
        '/customer/1',
        '/customer/1/contact'
      ]

arr.forEach(a=>console.log(a.match(/^\/([^\/]*)/g)[0]));

How about this solution?

Comments