我正在嘗試測試一個location.pathname. 我正在使用正則表達式傳遞一個函數的路徑。路徑可以是:/dashboard/dashboard/orders并且location.pathname可以是:/dashboard/dashboard/orders/dashboard/orders/5ea5c95684c28d4b7ff688e1/dashboard/orders/anyOtherId我想要做的是傳遞時,/dashboard/orders/5ea5c95684c28d4b7ff688e1因為location.pathname它只與path /dashboard/orders而不匹配/dashboard。在https://regexr.com/中測試正則表達式我可以使它工作:\/dashboard\/*,然后我嘗試在 javascript 中實現它:const matcher = new RegExp(`${path}\/*`)matcher.test(location.pathname)但是,當我通過 a path = /dashboard/ordersand時,location.pathname = /dashboard/orders/5ea5c95684c28d4b7ff688e1它與/dashboardand匹配/dashboard/orders。我需要它只與/dashboard/orders. 只有當is和/dashboard時才必須匹配。path/dashboardlocation.pathname = /dashboard我怎樣才能做到這一點?提前致謝編輯:為了更清楚,我需要它在pathis/dashboard/orders和location.pathnameis/dashboard/orders或時返回 true /dashboard/orders/5ea5c95684c28d4b7ff688e1。但它必須在pathis/dashboard和location.pathnameis/dashboard/orders或時返回 false/dashboard/orders/5ea5c95684c28d4b7ff688e1
傳遞變量時匹配正則表達式
呼如林
2022-07-21 20:59:50