加入收藏 | 设为首页 | 会员中心 | 我要投稿 李大同 (https://www.lidatong.com.cn/)- 科技、建站、经验、云计算、5G、大数据,站长网!
当前位置: 首页 > 百科 > 正文

正则表达式一

发布时间:2020-12-14 00:56:07 所属栏目:百科 来源:网络整理
导读: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

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',serif; font-size:12px; line-height:1.8em"> 'pathname':'index.html',serif; font-size:12px; line-height:1.8em"> 'query':'key1=1&key2=2'
  4. }
复制代码
参考答案:

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

(编辑:李大同)

【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容!

    推荐文章
      热点阅读