Regex for string containing one string, but not another

10,321

Solution 1

If lookarounds are supported, this is very easy to achieve:

(?=.*/pdf/)(?!.*help)(.+)

See a demo on regex101.com.

Solution 2

(?:^|\s)((?:[^h ]|h(?!elp))+\/pdf\/\S*)(?:$|\s)

First thing is match either a space or the start of a line

(?:^|\s)

Then we match anything that is not a or h OR any h that does not have elp behind it, one or more times +, until we find a /pdf/, then match non-space characters \S any number of times *.

((?:[^h ]|h(?!elp))+\/pdf\/\S*)

If we want to detect help after the /pdf/, we can duplicate matching from the start.

((?:[^h ]|h(?!elp))+\/pdf\/(?:[^h ]|h(?!elp))+)

Finally, we match a or end line/string ($)

(?:$|\s)

The full match will include leading/trailing spaces, and should be stripped. If you use capture group 1, you don't need to strip the ends.

Example on regex101

Share:
10,321
Jacob Petersen
Author by

Jacob Petersen

I like to tell myself that I know a little bit about JavaScript

Updated on June 15, 2022

Comments

  • Jacob Petersen
    Jacob Petersen almost 2 years

    Have regex in our project that matches any url that contains the string "/pdf/":

    (.+)/pdf/.+
    

    Need to modify it so that it won't match urls that also contain "help"

    Example:

    Shouldn't match: "/dealer/help/us/en/pdf/simple.pdf" Should match: "/dealer/us/en/pdf/simple.pdf"