3

I have those strings:

"/page/test/myimg.jpg"
"/page/test/"
"/page2/test/"
"/page/test/other"

I want true for all strings starting with /page/test except when it ends with .jpg.

Then I did: /^\/page\/test(.*)(?!jpg)$/. Well, it's not working. :\

It should return like this:

"/page/test/myimg.jpg" // false
"/page/test/" // true
"/page2/test/" // false
"/page/test/other" // true
Bohemian
  • 412,405
  • 93
  • 575
  • 722
Ratata Tata
  • 2,781
  • 1
  • 34
  • 49

2 Answers2

4

Easily done with JavaScript:

/^(?!.*\.jpg$)\/page\/test/
ridgerunner
  • 33,777
  • 5
  • 57
  • 69
3

Use a negative look behind anchored to end:

/^\/page\/test(.*)(?<!\.jpg)$/

For clarity, this regex will match any input that *doesnt end in .jpg:

^.*(?<!\.jpg)$

Edit (now must work in JavaScript too)

JavaScript doesn't support look behinds, so this ugly option must be used, which says that at least one of the last 4 characters must be other than .jpg:

^.*([^.]...|.[^j]..|..[^p].|...[^g])$
Bohemian
  • 412,405
  • 93
  • 575
  • 722