3

我有这些字符串:

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

我希望所有以 /page/test 开头的字符串都为 true ,除非它以.jpg.

然后我做了:/^\/page\/test(.*)(?!jpg)$/。好吧,它不工作。:\

它应该像这样返回:

"/page/test/myimg.jpg" // false
"/page/test/" // true
"/page2/test/" // false
"/page/test/other" // true
4

2 回答 2

4

使用 JavaScript 轻松完成:

/^(?!.*\.jpg$)\/page\/test/
于 2013-08-23T23:24:00.337 回答
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])$
于 2013-08-23T16:30:52.793 回答