正则表达式一

来源:互联网 发布:再生聚酯切片进口数据 编辑:程序博客网 时间:2024/06/16 00:35

MSDN官方文件参考:https://msdn.microsoft.com/zh-cn/library/az24scfc.aspx

eg:

1.判断字符串是否是这样组成的,第一个必须是字母,后面可以是字母、数字、下划线,总长度为5-20

  var reg = /^[a-zA-z]\w[4,19]/; (\w 视情况 更换 \s);

  reg.test(str);

2.编写一个javascript的函数把url解析为与页面的javascript.location对象相似的实体对象,如:url :'http://www.qq.com/index.html?key1=1&key2=2',最后输出的对象是

  1. {
  2. 'protocol':'http',
  3. 'hostname':'www.qq.com',
  4. 'pathname':'index.html',
  5. 'query':'key1=1&key2=2'
  6. }
复制代码
参考答案:

  1. var mylocation = {
  2.         'protocol':'http',
  3.         'hostname':'',
  4.         'pathname':'',
  5.         'query':''
  6.     }
  7.     var url = 'http://www.qq.com/index.html?key1=1&key2=2';
  8.     var str=url.replace(/http\:\/\//,""); //过滤 http://
  9.     var a=str.split("/");  // 以“/”分割url 存入数组
  10.     mylocation.hostname=a[0]; 
  11.     var arr=a[1].split("?"); 
  12.     mylocation.pathname=arr[0];
  13.     mylocation.query=arr[1];
  14.     console.log(mylocation);


0 0