Negative lookahead Regular Expression

前端 未结 7 938
后悔当初
后悔当初 2020-11-27 14:00

I want to match all strings ending in \".htm\" unless it ends in \"foo.htm\". I\'m generally decent with regular expressions, but negative lookaheads have me stumped. Why

7条回答
  •  盖世英雄少女心
    2020-11-27 14:40

    As mentioned JavaScript does not support negative look-behind assertions.

    But you could use a workaroud:

    /(foo)?\.htm$/i.test("/foo.htm") && RegExp.$1 != "foo";
    

    This will match everything that ends with .htm but it will store "foo" into RegExp.$1 if it matches foo.htm, so you can handle it separately.

提交回复
热议问题