vuejs / router

🚦 The official router for Vue.js
https://router.vuejs.org/
MIT License
3.88k stars 1.18k forks source link

Regex in params extract extra characters with any capturing groups #2178

Closed spsquared closed 6 months ago

spsquared commented 6 months ago

Reproduction

https://codesandbox.io/p/sandbox/vue-router-params-regex-bug-kdcvvs

Steps to reproduce the bug

  1. Create a router with a route that contains any regexp with capturing groups within it (like (.))
  2. Error as the regexp extracts the parenthesis from the route syntax

Expected behavior

The router extracts the regexp from inside the parenthesis and uses (.) as its regexp.

Actual behavior

An error occurs as the syntax is included as part of the extracted regexp.

Additional information

(.) is a fairly dumb expression but (a[abc])+ also breaks it.

posva commented 6 months ago

the () is already a regex group and the ) marks the end. Therefore you need to escape the closing ): https://paths.esm.dev/?p=AAMsIPQgYAEL9lNgQAECUgPgDIFoDiiAwA5sbTACwBgAXQR0GqDkngCA&t=/abacac#. Note in JS strings, you need to escape the \ too