For a web app I\'m trying to come up with a javascript regex that matches anything not ending in .json
. Sounds simple but I\'m finding it pretty damn hard.
Try /^(?!.*\.json$).*$/
/^(?!.*\.json$).*$/.test("foo.json")
false
/^(?!.*\.json$).*$/.test("foo")
true
/^(?!.*\.json$).*$/.test("foo.html")
true
maybe instead of testing "match (not .json)" you could test "not match (.json)", which is easy ?
You can always just get the file extension and then compare it.
Regex to find file extension
/\.[^.]*$/
get the file extension with
var extension = /\.[^.]*$/.exec("something.json");
if(extension[0] === ".json"){
//do something
}
I would create a regex to match .json and then when you check for it reverse the logic with a
match == false
That would be much simpler and it shows what you are trying to do making it a lot more obvious to other programmers.