Sunday, May 19, 2024
 Popular · Latest · Hot · Upcoming
4
rated 0 times [  10] [ 6]  / answers: 1 / hits: 52302  / 13 Years ago, wed, july 27, 2011, 12:00: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 doesn't this work?



/(?!foo).htm$/i.test(/foo.htm);  // returns true. I want false.


What should I be using instead? I think I need a negative lookbehind expression (if JavaScript supported such a thing, which I know it doesn't).


More From » regex

 Answers
18

The problem is pretty simple really. This will do it:


/^(?!.*foo.htm$).*.htm$/i.test("/foo.htm"); // returns false

[#90959] Wednesday, July 27, 2011, 13 Years  [reply] [flag answer]
Only authorized users can answer the question. Please sign in first, or register a free account.
deonkalvinw

Total Points: 409
Total Questions: 96
Total Answers: 89

Location: Saint Pierre and Miquelon
Member since Sun, Nov 27, 2022
2 Years ago
deonkalvinw questions
Sun, Feb 6, 22, 00:00, 2 Years ago
Tue, Dec 28, 21, 00:00, 2 Years ago
Sun, Aug 22, 21, 00:00, 3 Years ago
Sun, Mar 7, 21, 00:00, 3 Years ago
;