I am trying to match a sub directory in a URL that comes after a specific directory:
then append a directory to the matched string.
/applications/app1
should be /applications/app1/beta
/applications/app2/
should be /applications/app2/beta/
/applications/app2/settings
should be /applications/app2/beta/settings
/applications/app3?q=word
should be /applications/app3/beta?q=word
I wrote this:
path = path.replace(/(\/applications\/(.*)(\/|\s|\?))/, '$1/beta');
But doesn't work if the app-name is in the end of the string.
Note: I don't have the app name I only know that it follows /applications/
path.replace(/(\/applications\/[^/?]+)/g,'$1/beta');
After some consideration, I prefer the following:
path.replace(/(\/applications\/[^/?]+)($|\/|\?)(?!beta)/g,'$1/beta$2');
"/applications/app1/beta" -> "/applications/app1/beta"
"/applications/app1" -> "/applications/app1/beta"
"/applications/app1/settings" -> "/applications/app1/beta/settings"
"/applications/app1?q=123" -> "/applications/app1/beta?q=123"
It will ignore /applications/beta
when matching.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With