javascript - NOT a specific word at ending in regex - Stack Overflow

I have those strings: "pagetestmyimg.jpg""pagetest""page2test"

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

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
Share Improve this question edited Aug 23, 2013 at 17:38 Bohemian 426k102 gold badges602 silver badges746 bronze badges asked Aug 23, 2013 at 16:27 Ratata TataRatata Tata 2,8791 gold badge37 silver badges49 bronze badges 3
  • You can't really do negative look ups like that without doing a bit of trickery. Take a look at stackoverflow./questions/406230/… where this has been answered thoroughly. – ranieuwe Commented Aug 23, 2013 at 16:29
  • What language are we talking about here? – Madara's Ghost Commented Aug 23, 2013 at 16:30
  • @MadaraUchiha .htaccess – Ratata Tata Commented Aug 23, 2013 at 16:32
Add a ment  | 

2 Answers 2

Reset to default 4

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

Easily done with JavaScript:

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

发布者:admin,转转请注明出处:http://www.yc00.com/questions/1744688301a4588054.html

相关推荐

发表回复

评论列表(0条)

  • 暂无评论

联系我们

400-800-8888

在线咨询: QQ交谈

邮件:admin@example.com

工作时间:周一至周五,9:30-18:30,节假日休息

关注微信