正则表达式,用于检查URL是否为主URL

Arr*_*und 2 javascript regex url

我正在寻找一个正则表达式来检查URL是否是主页URL.

有效

http://example.com
https://example1.com
https://example2.com/
http://anything.com/?test=12
http://anything.com?test=12
Run Code Online (Sandbox Code Playgroud)

无效

http://example.com/path
https://example.com/path1/path2
https://example.com/path1?test=123
Run Code Online (Sandbox Code Playgroud)

anu*_*ava 5

你可以使用这个正则表达式:

/^https?:\/\/[^\/]+\/?(\?.*)?$/gm
Run Code Online (Sandbox Code Playgroud)

RegEx演示