Tom Rudge
Tom Rudge

Reputation: 3272

Regex to allow for anystring in url

const profileActive = location.pathname && location.pathname.match(/^\/app\/profile/) ? "profileActive" : "";

My case need to match:

/app/profile/ANYTHINGHERE/report

At the moment it matches just:

/app/profile/

Any help to express a match anything expression between / /

Thanks

Upvotes: 1

Views: 33

Answers (1)

Wiktor Stribiżew
Wiktor Stribiżew

Reputation: 626927

You may use either

/^\/app\/profile(?:\/.*)?\/report$/

See this regex demo

Or, if there should only be 1 subpart in between:

/^\/app\/profile(?:\/[^\/]+)?\/report$/

See this regex demo.

The (?:\/[^\/]+)? will match an optional sequence of / and 1+ chars other than / while (?:\/.*)? will match an optional sequence of a / followed with any 0+ chars.

Upvotes: 1

Related Questions