对于一个网络应用程序,我正在尝试提出一个javascript正则表达式匹配任何没有结束的东西.json.听起来很简单,但我发现它非常难受.
我首先想要这样做:^.*(?!\.json$),但这显然不起作用,因为它只是匹配整个字符串.然后我尝试^[^\.]*(?!\.json$),但匹配ab在abc.json.
我到目前为止已经提出了两个正在执行此工作的正则表达式,但我希望有一个可以执行此操作的正则表达式.
// Match anything that has a dot but does not end in .json
^.*\.(?!json$)
// Match anything that doesn't have a dot
^[^\.]*$
Run Code Online (Sandbox Code Playgroud)
我喜欢http://regex101.com/#javascript来测试它们.
我使用正则表达式作为ExpressJS路由定义的一部分app.get(REGEXP, routes.index).