Home > Enterprise >  Regex detect last character slash and and ?id=
Regex detect last character slash and and ?id=

Time:02-21

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= ?

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

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

CodePudding user response:

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*

  • Related