文章详情页
正则表达式 - javascript正则列出链接中的字符串
浏览:81日期:2023-05-29 16:34:49
问题描述
http://www.xxx.com/one/two/three/four
将链接中每个 / 后面的字符查找出来,放到一个数组里,如: [’one’,’two’,’three’,’four’] 链接长度不限制。正则该怎么写?
问题解答
回答1:是当前页面url: window.location.pathname.substr(1).split(’/’)
不是当前页面url:url.replace(/http(s){0,1}://[^/]+//, ’’).split(’/’)
回答2:window.location.pathname.split(’/’)
回答3:楼上的思路不错,把前面的host去掉, 剩下的用/进行分割,更简单一点
-------以下是答案
这个需要用到断言
const str = ’http://www.xxx.com/one/two/three/four’;const result = str.match(/(?/[/])w+/g).map((item) => { return item.substr(1);});// 输出// ['www', 'one', 'two', 'three', 'four']
标签:
JavaScript
相关文章:
1. MySQL中无法修改字段名的疑问2. angular.js - angularjs的自定义过滤器如何给文字加颜色?3. docker镜像push报错4. angular.js - angular内容过长展开收起效果5. javascript - 如何让移动端网页的输入框固定在底部?6. 请教各位大佬,浏览器点 提交实例为什么没有反应7. python的前景到底有大?如果不考虑数据挖掘,机器学习这块?8. python - flask表单 如何把提交多行数据在服务端读取出来?9. 大家好,请问在python脚本中怎么用virtualenv激活指定的环境?10. 网页爬虫 - 用Python3的requests库模拟登陆Bilibili总是提示验证码错误怎么办?
排行榜
