要当安静的女子

导航

匹配url的正则表达式

在javascript语言精粹 看到url的正则,将其记录下来

var parse_url = /^(?:([A-Za-z]+):)?(\/{0,3})([0-9.\-A-Za-z]+)(?::(\d+))?(?:\/([^?#]*))?(?:\?([^#]*))?(?:#(.*))?$/;

var url = "http://www.ora.com:80/goodparts?q#fragment";

执行 具体代码见下述:

var url = "http://www.ora.com:80/goodparts?q#fragment";
var parse_url = /^(?:([A-Za-z]+):)?(\/{0,3})([0-9.\-A-Za-z]+)(?::(\d+))?(?:\/([^?#]*))?(?:\?([^#]*))?(?:#(.*))?$/;
var result = parse_url.exec(url);
var names = ['url','scheme','slash','host','port','path','query','hash'];
var blanks = ' ';
var i;
for (i = 0; i < names.length; i += 1){
  document.writeln(names[i] + ':' + blanks.substring(names[i].length), result[i]);
}

执行结果:

url: http://www.ora.com:80/goodparts?q#fragment
scheme: http
slash: //
host: www.ora.com
port: 80
path: goodparts
query: q
hash: fragment

 

关于上述url的正则的部分解释:

 /^(?:([A-Za-z]+):)?(\/{0,3})([0-9.\-A-Za-z]+)(?::(\d+))?(?:\/([^?#]*))?(?:\?([^#]*))?(?:#(.*))?$/

1.^表示开始   $表示结尾

 

 /^(?:([A-Za-z]+):)?(\/{0,3})([0-9.\-A-Za-z]+)(?::(\d+))?(?:\/([^?#]*))?(?:\?([^#]*))?(?:#(.*))?$/

2. (?:pattern) 表示匹配pattern但不获取匹配结果

   (?:([A-Za-z]+):) 则表示匹配 ([A-Za-z]+): ,但只返回匹配到的([A-Za-z]+)的结果,并不返回:

 

 /^(?:([A-Za-z]+):)?(\/{0,3})([0-9.\-A-Za-z]+)(?::(\d+))?(?:\/([^?#]*))?(?:\?([^#]*))?(?:#(.*))?$/

3. \*{3},因为*是特殊字符,所以用转义字符,该表达式表示匹配3个*,即***

   同理\/{0,3}表示匹配0到3个/,即没有/或者/, //, ///

 

 /^(?:([A-Za-z]+):)?(\/{0,3})([0-9.\-A-Za-z]+)(?::(\d+))?(?:\/([^?#]*))?(?:\?([^#]*))?(?:#(.*))?$/

4. 方括号中的^表示 非 的意思

  [^?#] 则表示 非 ? 且 非 # 的其他字符

 

 /^(?:([A-Za-z]+):)?(\/{0,3})([0-9.\-A-Za-z]+)(?::(\d+))?(?:\/([^?#]*))?(?:\?([^#]*))?(?:#(.*))?$/

* 匹配前一项0次或多次。即{0,}

?匹配前一项0次或1次。即{0,1}

 

posted on 2017-09-28 12:40  码啾码  阅读(27011)  评论(0编辑  收藏  举报