Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

Regex detect last character slash and and ?id=

This is my Javascript regex ^.*(?<!\/)$ that checks if the URL is not ending with (/) slash

detects: /play/popular
ok: /play/popular/

How can I detect if the URL is not ending with / and does not include ?id= ?

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

detects: play/popular
catch: play/popular/?id=448

debugger: https://regex101.com/r/sSDIic/2

>Solution :

One way is to extend the negative lookbehind to optionally add the id= followed by 1 or more digits.

^.*$(?<!\/(?:\?id=\d+)?)

Regex demo

It might be easier to rule out id= in the string using a negative lookahead, and match a non whitespace char at the end being not a /if it can not end on a space.

^(?!.*\?id=).*[^\s/]$

Regex demo

Note that .* also match spaces, to match only non whitespace characters you can use \S*

Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading