-1

I want to validate the url end point using regex. Example end point like: /user/update.

First I tried with (/[A-Za-z0-9_.:-~/]*) but also matches http://url.com/user/update with javascript regex. I want the string to only validate pass if it is equal to /user/update like end points

Pranu Pranav
  • 353
  • 3
  • 8
  • Does this answer your question? [Javascript regular expression to validate URL](https://stackoverflow.com/questions/8667070/javascript-regular-expression-to-validate-url) – Yogi Oct 16 '22 at 06:21
  • No @Yogi it explains how to validate full url. I want to validate the string which only contain endpoint. – Pranu Pranav Oct 16 '22 at 06:28

2 Answers2

0

You can use regex look behind technique to get the path after the .com with /(?<=.com).*/

const matchEndPoint = (str) => str.match(/(?<=.com).*/)

const [result] = matchEndPoint('http://url.com/user/update');

console.log(result)
Mina
  • 14,386
  • 3
  • 13
  • 26
0

You might use a pattern like

^\/[\w.:~-]+\/[\w.:~-]+$

Regex demo

Or for example not allowing consecutive dashes like -- and match one or more forward slashes:

^\/\w+(?:[.:~-]\w+)*(?:\/\w+(?:[.:~-]\w+)*)*$

Explanation

  • ^ Start of string
  • \/\w+ Match / and 1+ word chars
  • (?:[.:~-]\w+)* Optionally repeat a char of the character class and 1+ word chars
  • (?: Non capture group
    • \/\w+ Match / and 1+ word chars
    • (?:[.:~-]\w+)* Optionally repeat a char of the character class and 1+ word chars
  • )* Close group and optionally repeat
  • $ End of string

Regex demo

The fourth bird
  • 154,723
  • 16
  • 55
  • 70