3

I need help with a route pattern. I have URLs like this:

http://example.com/product/product-title-51
http://example.com/product/another-product-title-137
http://example.com/product/another-product-45-title-with-number-288-anywhere-178

...

I need to write a path pattern which will match the last number of a slug. That number is a product ID.

const router = new VueRouter({
    routes: [{
        path: '/product/:product',
        component: PageProduct,
        name: 'Product'
    }]
})

What pattern can I use for this case? Or would you suggest a better solution for eShop product slug?

0

1 Answer 1

4

You can create a path expression that accepts any number of characters between /product/ and the last number like this.

path: '/product/(.*-)?:product(\\d+)'

Demo ~ https://jsfiddle.net/fs8zyx22/2/

This has the added bonus of being able to support a number-only link like /product/123.

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.