首页 > 解决方案 > 如何从由斜杠分隔的 url 中获取 url 参数,例如 http://myweb.com/example/12345/222/4444 我想从上面的 url 中获取 12345、222、4444

问题描述

试图获取最后一个参数以进行进一步处理,但无法将它们分开。

标签: javascriptstringreactjspattern-matching

解决方案


您可以在获取urlsplit后使用 JS 的功能来做到这一点。pathname

const url = 'http://myweb.com/12345/222/4444';
const a = document.createElement('a');
a.href = url;

const pathname = a.pathname.substring(1);
console.log(pathname.split('/'));


推荐阅读