我正在尝试进行一些基本的URL清理,以便于...
www.google.com
www.google.com/
http://google.com
http://google.com/
https://google.com
https://google.com/
被替换为http://www.google.com
(或在https://
开头的情况下为https://www.google.com
)。
基本上,我想检查一个正则表达式中是否有http/https
开头和/
结尾。
我尝试了这样的代码:
"https://google.com".match(/^(http:\/\/|https:\/\/)(.*)(\/)*$/)
在这种情况下,我得到:
=> #<MatchData "https://google.com" 1:"https://" 2:"google.com" 3:nil>
这是好的。
不幸的是,对于:
"https://google.com/".match(/^(http:\/\/|https:\/\/)(.*)(\/)*$/)
我得到:
=> #<MatchData "https://google.com/" 1:"https://" 2:"google.com/" 3:nil>
,但我想要 2:"google.com" 3:"/"
有什么想法吗?