Currently, I am grappling with creating a JavaScript regex for a web application that identifies anything not ending in .json
. It seems like a straightforward task, but it is proving to be quite challenging.
Initially, my approach was using this regex pattern: ^.*(?!\.json$)
, however, it only matched the entire string. Next, I attempted ^[^\.]*(?!\.json$)
which ended up matching ab
in abc.json
.
I have managed to come up with two separate regex patterns that accomplish the task individually, but I am determined to consolidate them into a single regex.
// Identifies anything with a dot except for those ending in .json
^.*\.(?!json$)
// Identifies anything without a dot
^[^\.]*$
To test these regex patterns, I utilize http://regex101.com/#javascript.
In my ExpressJS route definition, I implement the regexp within app.get(REGEXP, routes.index)
.