Create an account

Very important

  • To access the important data of the forums, you must be active in each forum and especially in the leaks and database leaks section, send data and after sending the data and activity, data and important content will be opened and visible for you.
  • You will only see chat messages from people who are at or below your level.
  • More than 500,000 database leaks and millions of account leaks are waiting for you, so access and view with more activity.
  • Many important data are inactive and inaccessible for you, so open them with activity. (This will be done automatically)


Thread Rating:
  • 484 Vote(s) - 3.6 Average
  • 1
  • 2
  • 3
  • 4
  • 5
Regex that matches anything not ending in .json

#1
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.

I first wanted to do it like this: `^.*(?!\.json$)`, but that obviously didn't work as it simply matches the entire string. Then I tried `^[^\.]*(?!\.json$)` but that matches `ab` in `abc.json`.

I've come so far as to come up with two regexes that do the job, but I want to have one regex that can do this.


// Match anything that has a dot but does not end in .json
^.*\.(?!json$)
// Match anything that doesn't have a dot
^[^\.]*$


I like

[To see links please register here]

to test them.

I am using the regexp as part of ExpressJS route definition in ``app.get(REGEXP, routes.index)``.
Reply

#2
Try `/^(?!.*\.json$).*$/`

/^(?!.*\.json$).*$/.test("foo.json")
false
/^(?!.*\.json$).*$/.test("foo")
true
/^(?!.*\.json$).*$/.test("foo.html")
true
Reply

#3
maybe instead of testing "match (not .json)" you could test "not match (.json)", which is easy ?
Reply

#4
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.
Reply

#5
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
}
Reply



Forum Jump:


Users browsing this thread:
1 Guest(s)

©0Day  2016 - 2023 | All Rights Reserved.  Made with    for the community. Connected through