简体   繁体   中英

NOT a specific word at ending in regex

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

使用JavaScript轻松完成:

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

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])$

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM