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
CodePudding user response:
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)
CodePudding user response:
You might use a pattern like
^\/[\w.:~-] \/[\w.:~-] $
Or for example not allowing consecutive dashes like -- and match one or more forward slashes:
^\/\w (?:[.:~-]\w )*(?:\/\w (?:[.:~-]\w )*)*$
Explanation
^Start of string\/\wMatch / and 1 word chars(?:[.:~-]\w )*Optionally repeat a char of the character class and 1 word chars(?:Non capture group\/\wMatch / 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
