2009-11-09 3 views

risposta

42

Prova questo- utilizzo

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 ")) alert (" è corretto ") else alert (" non corretto");

+0

Hi @MarkRedman, la scheda snippets.dzone che si è legato alla sembra essere stato spostato (ricevo un messaggio di errore finiti 410). Esiste una versione aggiornata di esso? Grazie! –

8

provare qualcosa di simile:

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

La funzione isUrl() non funziona per "http: // localhost/myprojects", "http://127.0.0.1/myprojects" – Code

+1

Questo non funziona nemmeno su "http://google.com". .. – christianbundy

+0

Non lavorare per "http: //localhost/jquery.colorhelpers.js" –

8
function IsURL(url) { 

    var strRegex = "^((https|http|ftp|rtsp|mms)?://)" 
     + "?(([0-9a-z_!~*'().&=+$%-]+:)?[0-9a-z_!~*'().&=+$%-][email protected])?" //ftp的[email protected] 
     + "(([0-9]{1,3}\.){3}[0-9]{1,3}" // IP形式的URL- 199.194.52.184 
     + "|" // 允许IP和DOMAIN(域名) 
     + "([0-9a-z_!~*'()-]+\.)*" // 域名- www. 
     + "([0-9a-z][0-9a-z-]{0,61})?[0-9a-z]\." // 二级域名 
     + "[a-z]{2,6})" // first level domain- .com or .museum 
     + "(:[0-9]{1,4})?" // 端口- :80 
     + "((/?)|" // a slash isn't required if there is no file name 
     + "(/[0-9a-z_!~*'().;?:@&=+$,%#-]+)+/?)$"; 
    var re=new RegExp(strRegex); 
    return re.test(url); 
} 

Regular expression visualization

Debuggex Demo (Improved version which matches also 'localhost')

+0

Questo è stato il migliore che ho trovato finora. L'unica cosa, ritorna true a isURL ("http"). – Anoyz

+2

molto bello. 'Https | http | ftp ...' può scritto più breve => 'https | ftp ...?' – algorhythm

+0

Versione migliorata per abbinare anche 'localhost': aggiungere '| localhost' dopo '+ "[az] {2,6 })" .com Dominio // primo livello o .museum' all'interno della staffa => '+ "[az] {2,6} | localhost)" // primo livello .com Dominio o .museum' – algorhythm

0

Uno per il futuro con il URL constructor e una dichiarazione di base try catch, si è supportato nella maggior parte dei browser moderni. Ovviamente nessun supporto IE ...

const isUrl = (str) => { 
    try { 
    new URL(str); 
    return true; 
    } catch() { 
    return false; 
    } 
} 

Se l'URL è valido verrà analizzato dal costruttore e restituirà true.

Se la stringa non è un URL valido il costruttore sarà Chuck un errore di sintassi che farsi prendere e restituire false.

+0

Si noti che questo passerà tecnicamente come valido con il costruttore di URL: 'http: // www.' – Prefix

2

provare questo codice. Questa espressione è più completo e tiene conto indirizzo IP:

function checkUrl(s) { 
    var regexp = /^(?:http(s)?:\/\/)?[\w.-]+(?:\.[\w\.-]+)+[\w\-\._~:/?#[\]@!\$&'\(\)\*\+,;=.]+$/ 
    return regexp.test(s); } 
Problemi correlati