• 匹配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}

  • 相关阅读:
    C++中几种字符串表示方法
    oracle11g卸载(win10)
    Dbvisualizer 连接oracle数据库
    严重: Exception starting filter struts2 Unable to load configuration.
    eclipse启动Tomcat服务输入http://localhost:8080/报404
    Tomcat内存溢出解决办法
    A Java Runtime Environment(JRE) or Java Development Kit (JDK) must be available in order to run Eclipse.
    Spring容器装配bean的方式
    Spring容器的基本实现
    spring环境搭建
  • 原文地址:https://www.cnblogs.com/xnn1993/p/7606189.html
Copyright © 2020-2023  润新知