如何测试 URL 字符串是绝对的还是相对的? [英] How to test if a URL string is absolute or relative?
本文介绍了如何测试 URL 字符串是绝对的还是相对的?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
如果 URL 是 Javascript 或 jQuery 中的相对或绝对路径,我如何测试它?我想根据传入的 URL 是本地路径还是外部路径进行相应处理.
How can I test a URL if it is a relative or absolute path in Javascript or jQuery? I want to handle accordingly depending if the passed in URL is a local or external path.
if (urlString starts with http:// or https://)
//do this
推荐答案
var pat = /^https?:///i;
if (pat.test(urlString))
{
//do stuff
}
对于协议相关的 url,使用这个正则表达式:
For protocol relative urls, use this regex:
/^https?://|^///i
这篇关于如何测试 URL 字符串是绝对的还是相对的?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文