Hemadri Dasari
Hemadri Dasari

Reputation: 33994

JavaScript regular expression to validate only path params in URL

I'm trying to validate path params in the url for the below scenario.

I have a text box wr user will input path params and the text box should contain only path params as like below

/{id}/{name}

I need to validate using regex expression whether the input contains forward slash with curly braces. if they are not then regex should fail.

Upvotes: 1

Views: 327

Answers (2)

Dan Nagle
Dan Nagle

Reputation: 5425

Something like this:

^(\/{(\w)+})+$/i

You could replace the \w with [a-z0-9] if you want to limit the params to alpha numeric values

^(\/{(\w)+})+\/?$/i

This will accept the trailing slash e.g. /{id}/{name}/

Upvotes: 2

dislick
dislick

Reputation: 677

It's important to know what characters you want to allow within id and name. This regex allows everything except / and {.

^\/{[^\/{]+?}\/{[^\/{]+?}$

And this one only allows a-z, A-Z, _, -:

^\/{[\w-]+?}\/{[\w-]+?}$

Upvotes: 0

Related Questions