正则表达式的解释

时间:2013-10-29 05:56:57

标签: javascript php regex

任何人都可以解释这个正则表达式的含义吗?

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

2 个答案:

答案 0 :(得分: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

我错了?

答案 1 :(得分: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);

url="https://www.xyz.x"时返回false,url="https://www.xyz.xe"

时返回true