JavaScript regular expression to validate only path params in URL
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.
2 Answers
2
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
[a-z0-9]
^(/(w)+)+/?$/i
This will accept the trailing slash e.g. /id/name/
/id/name/
It's important to know what characters you want to allow within id and name. This regex allows everything except / and {.
id
name
/
{
^/{[^/]+?/{[^/]+?$
And this one only allows a-z, A-Z, _, -:
a-z
A-Z
_
-
^/[w-]+?/[w-]+?$
I want to allow characters, numbers,/ and . Other special characters should be restricted
– Hemadri Dasari
Aug 9 at 7:20
Thanks for contributing an answer to Stack Overflow!
But avoid …
To learn more, see our tips on writing great answers.
Some of your past answers have not been well-received, and you're in danger of being blocked from answering.
Please pay close attention to the following guidance:
But avoid …
To learn more, see our tips on writing great answers.
Required, but never shown
Required, but never shown
By clicking "Post Your Answer", you acknowledge that you have read our updated terms of service, privacy policy and cookie policy, and that your continued use of the website is subject to these policies.
Dan thanks for your quick help. This one ^(/(w)+)+$/i worked for me after appending forward slash in the beginning like /^(/(w)+)+$/i. Is there any way to find duplicates in path /id/name/id using same regex
– Hemadri Dasari
Aug 9 at 7:18