Home > Software engineering >  JS regex pathname match
JS regex pathname match

Time:05-16

i'm a bit lost about regex. What I want is to create is a simple check and match a path. (regex noob)

To clarify;

The application can have sub directories

/
/dashboard/modules
/modules/acounting/
/modules/acounting/stats
/modules/invoicing/
/modules/invoicing/invoices

What I want to accomplish is to match against everything from first forward slash to slash except the pages.

/
/dashboard/
/modules/acounting/
/modules/invoicing/

Tried //([^/] )/[^/]*$/)

const url = window.location;
url.pathname.match(/\/([^/] )\/[^/]*$/)

It creates a array. Regex help appreciated

CodePudding user response:

You could simply use .*\/

Regex101 example and description

If you want to filter-match sections (vs. pages), use: .*\/$

Regex101 example and description

  • Related