Home > Net >  Regex to capture everything if word is detected
Regex to capture everything if word is detected

Time:07-19

I am trying to capture this url https://www.example.com/admin

I currently use this rule which matches everything unfortunately

(https?:\/\/)?([\da-z\.-] )\.([a-z]{2,6})([\/\w\.-]*)*\/?

my question is how can I match every URL that has /admin in it for example /admin/a/1, /admin/2/b

CodePudding user response:

Converting my comment to answer so that solution is easy to find for future visitors.

You may use this regex to match URLs that either end with /admin or have /admin/ in them:

^(https?:\/\/)?\S*\/admin(\/.*)?$

RegEx Demo

You may want to avoid capture groups and use all non-capture groups:

^(?:https?:\/\/)?\S*\/admin(?:\/\S*)?$

RegEx Details:

  • ^: Start
  • (https?:\/\/)?: Match optional http:// or https://
  • \S*: Match 0 or more non-whitespaces
  • \/admin: Match /admin
  • (\/\S*)?: Optionally match / followed by any non-whitespace text
  • $: End

CodePudding user response:

try this

^(https?:\/\/)?([\da-z\.-] )\.([a-z]{2,6})([\/\w\.-]*)\Wadmin\W. 
  • Related