you can't use string.test(regexp), it needs to be reversed regexp.test(string) - but I'm thinking best idea would be to drop regexp completely and use just .lastIndexOf because building regexps with filenames and escaping seems too pricy and unnecessary