如何使用javascript检测字符串是否为URL格式? [英] How to detect whether a string is in URL format using javascript?

查看:121
本文介绍了如何使用javascript检测字符串是否为URL格式?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我认为问题标题似乎解释了eveything。
我想用javascript检测字符串是否为URL格式。

I think the question title seems to explain eveything. I want to detect whether a string is in URL format or not using javascript.

任何帮助表示感谢。

推荐答案

试试这个 -

function isUrl(s) {
   var regexp = /(ftp|http|https):\/\/(\w+:{0,1}\w*@)?(\S+)(:[0-9]+)?(\/|\/([\w#!:.?+=&%@!\-\/]))?/
   return regexp.test(s);
}




用法:if(isUrl( http://www.page.com ))提醒(是正确的)否则
提醒()不正确);

usage: if (isUrl("http://www.page.com")) alert("is correct") else alert("not correct");

这篇关于如何使用javascript检测字符串是否为URL格式?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆