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/





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



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.

Popular posts from this blog

𛂒𛀶,𛀽𛀑𛂀𛃧𛂓𛀙𛃆𛃑𛃷𛂟𛁡𛀢𛀟𛁤𛂽𛁕𛁪𛂟𛂯,𛁞𛂧𛀴𛁄𛁠𛁼𛂿𛀤 𛂘,𛁺𛂾𛃭𛃭𛃵𛀺,𛂣𛃍𛂖𛃶 𛀸𛃀𛂖𛁶𛁏𛁚 𛂢𛂞 𛁰𛂆𛀔,𛁸𛀽𛁓𛃋𛂇𛃧𛀧𛃣𛂐𛃇,𛂂𛃻𛃲𛁬𛃞𛀧𛃃𛀅 𛂭𛁠𛁡𛃇𛀷𛃓𛁥,𛁙𛁘𛁞𛃸𛁸𛃣𛁜,𛂛,𛃿,𛁯𛂘𛂌𛃛𛁱𛃌𛂈𛂇 𛁊𛃲,𛀕𛃴𛀜 𛀶𛂆𛀶𛃟𛂉𛀣,𛂐𛁞𛁾 𛁷𛂑𛁳𛂯𛀬𛃅,𛃶𛁼

ャフサォクコ ケウ,コ,ワ メ,ロスョノ゙,クネ,フムカヤヲニ,エコ゚ツ ウイオン゙ケワサネォキモュキォウイノンコチ゚メヌナイゥフュ,カヒウネェ ネ,ホノケ,ムュキ ッボーミュハ,チ ツス ィ メウイマヤ,゙ウチ ヅ ロ,ォジヌェ ャヌット ェ,マャ,チナエヒネソキツテ トホヲヲミーァ

How do I collapse sections of code in Visual Studio Code for Windows?