Search code examples
angularjsregexregex-lookaroundsregex-groupregex-greedy

RegEx for matching URLs with trailing slash


I am trying to write an expression for AngularJs. I need to use it inside an ng-pattern directive so to put a validation constraint to a form.

What I actually need is a regex for a URL in https that has always to end with a slash: /.

It would be nice if it ends more specifically in /pre/last/

How do I solve this problem?


Solution

  • This RegEx might help us to match /pre/last/. It creates two groups, in case we wish to call those groups, we can do so using $2 for /pre/last/ and $1 for first the part of our URL (Please see the image).

     '/(.+)(\/pre\/last\/)/g'
    

    We might not want to bound it with start (^) or end ($) anchors, and it might still does our desired matchings.

    enter image description here

    This post explains how we would do so in JavaScript.