2013-10-29 98 views
-1

任何人都可以解釋這個正則表達式的含義是什麼?正則表達式的解釋

/^(https?):\/\/(www\.)?[a-zA-Z0-9\-\.]+\.[a-zA-Z0-9]{2,3}[a-zA-Z0-9\-\#\.\/\?]*$/ 
+0

請參閱 的回答「http://stackoverflow.com/questions/161738/what-is-the-best-regular-expression-to-check-if-a-string-is-a-valid-網址「 – Oded

+0

該功能是正確的,但不是你給的網址。所有這些都是無效的網址。 :-) –

+0

HTTPS://www.x.xx https://www.x.co 的https://www.x.c 這是真實的URL,爲什麼? –

回答

1

最後,我已經成立了我自己的解決方案:

function validateURL(url) 
{ 
    var re = /^(https?):\/\/(www\.)?[a-z0-9\-\.]+\.[[a-z0-9\-\.\/]{2,}]*$/; 
    if (!re.test(url)) 
    { 
     return false; 
    } 
    else 
    { 
     return true; 
    } 
} 

this function return true if your url contain these: 
part 1. https or http 
part 2. www or not (means optional). 
Below image exploring in more depth. 

enter image description here

我錯了嗎?

+0

* return re.test(url); * –

+0

你在哪裏生成了該圖像?謝謝。 –

0

我試過以下並且適合我。

var url = "https://www.xyz.xe"; 
function validateURL(url) 
{ 
    var urlregex = new RegExp("^(http|https)\://([a-zA-Z0-9\.\-]+(\:[a-zA-Z0-9\.&%\$\-]+)*@)*((25[0-5]|2[0-4][0-9]|[0-1]{1}[0-9]{2}|[1-9]{1}[0-9]{1}|[1-9])\.(25[0-5]|2[0-4][0-9]|[0-1]{1}[0-9]{2}|[1-9]{1}[0-9]{1}|[1-9]|0)\.(25[0-5]|2[0-4][0-9]|[0-1]{1}[0-9]{2}|[1-9]{1}[0-9]{1}|[1-9]|0)\.(25[0-5]|2[0-4][0-9]|[0-1]{1}[0-9]{2}|[1-9]{1}[0-9]{1}|[0-9])|([a-zA-Z0-9\-]+\.)*[a-zA-Z0-9\-]+\.(com|edu|gov|int|mil|net|org|biz|arpa|info|name|pro|aero|coop|museum|[a-zA-Z]{2}))(\:[0-9]+)*(/($|[a-zA-Z0-9\.\,\?\'\\\+&%\$#\=~_\-]+))*$"); 
    return urlregex.test(url); 
} 
validateURL(url); 

Returs假時url="https://www.xyz.x"和真當url="https://www.xyz.xe"